title: Getting input string not in a correct format error for Powershell variable in script
body: <p>I've a script that is supposed to read input from a user, and pass that to the script's function. The value is supposed to be of type System.Int64 and even though I have cast it as such, I still get the following error:</p>
  
  <blockquote>
    <p>Get-WinEvent : Cannot bind parameter 'MaxEvents'. Cannot convert value "$b" to type "System.Int64". Error: "Input string was not in a correct format."</p>
  </blockquote>
  
  <p>I added a line to output the type of the variable using:</p>
  
  <pre><code>Write-Host "Type of MaxEntries: " $maxEntries.GetType()
  </code></pre>
  
  <p>And it shows that it is System.Int64 but I still get the same error. Furthermore, if I remove the double-quotes from around the variable in the actual command within the function, I get this error:</p>
  
  <blockquote>
    <p>Get-WinEvent : Cannot validate argument on parameter 'MaxEvents'. The argument is null, empty, or an element of the argument collection contains a null value. 
    Supply a collection that does not contain any null values and then try the command again.</p>
  </blockquote>
  
  <p>So it gets passed as null in that case. Weirdly though, I write out the value stored inside the MaxEntries variable both outside and inside the function and it properly displays the value I entered.</p>
  
  <p>Below is the script:</p>
  
  <pre><code>$DesktopPath = [Environment]::GetFolderPath("Desktop")
  $path = "$DesktopPath\Hashes.txt"
  $apiKey = Read-Host -Prompt "Enter your VT api key"
  $password = Read-Host -Prompt "Enter your VT account password" | ConvertTo-SecureString -AsPlainText -Force
  Set-VTAPIKey -APIKey $apiKey -MasterPassword $password
  
  $PatternMD5 = '(?&lt;=MD5=)(.*)(?=,)'
  $PatternSHA256 = '(?&lt;=SHA256=)(.*)'
  
  
  #try{
  $days = Read-Host -Prompt "How far back would you like to search the logs (days)?"
  #} catch {
  #until
  #} (($days -ge 1 -and $days -lt 100))
  $daysBack = (Get-Date).AddDays(-$days)
  
  
  #try{
  [int64]$maxEntries = Read-Host -Prompt "What is the maximum number of log events you would like to retrieve? (1-100)?"
  #} catch{
  #until
  #} (($maxEntries -ge 1 -and $MaxEntries -lt 100))
  Write-Host "MaxEntries BEFORE function: " $maxEntries
  
  function GetHashes($a, $b){
  
      Write-Host "daysBack: " $daysBack
      Write-Host "MaxEntries AFTER function: " $maxEntries
      Write-Host "Type of MaxEntries: " $maxEntries.GetType()
      $events = Get-WinEvent -FilterHashtable @{logname="Microsoft-Windows-Sysmon/Operational";StartTime = $a} -MaxEvents $b
      ForEach ($event in $events) {
          if ($event.Message.Contains("Hashes")){
              Write-Host "Event contains hashes"
              # Create hashtable
              $Dictionary = @{}
              # Convert event message to string
              $string = $event.Message.ToString()
              # Split lines in event message based off newline, and remove any null values
              $string.Split([environment]::NewLine,[System.StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
                  #Split each pair into key and value based off ':' delimiter
                  $key,$value = $_.Split(':')
                  #Write-Host "Key: " $key
                  #Write-Host "Value: " $value
                  #Populate $Dictionary
                  $Dictionary[$key] = $value
                  }
             Write-Host "Hashes: " $Dictionary.Item("Hashes")
             $hashes = $Dictionary.Item("Hashes")
             $MD5Hash = ($hashes -match $patternMD5)
             $MD5Hash = $matches[0]
  
             $SHA256Hash = ($hashes -match $patternSHA256)
             $SHA256Hash = $matches[1]
  
             $SHA256Hash &gt;&gt; $path
             $MD5Hash &gt;&gt; $path
             #Write-Host "MD5 Hash is: " $MD5Hash
             #Write-Host "SHA256 Hash is: " $SHA256Hash
             Write-Host
             } 
          }
      }
  GetHashes $daysBack, $maxEntries
  Submit-VTFile $path
  </code></pre>
  
  <p>I want to prompt the user for the maximum number of log entries to be returned <code>$maxEntries</code> and then pass that value into <code>function GetHashes($a, $b)</code> where it ultimately gets used in this command:</p>
  
  <pre><code>$events = Get-WinEvent -FilterHashtable @{logname="Microsoft-Windows-Sysmon/Operational";StartTime = $a} -MaxEvents $b
  </code></pre>
  
  <p>I have a gut feeling this is something really silly and naive but I would appreciate a nudge or link to some heplful reading material.</p>
  
  <p>I suspect the following:</p>
  
  <ul>
  <li>casting the maxEntries variable incorrectly</li>
  <li>incorrect use of quotes</li>
  </ul>