title: How do I pass variables with the Invoke-Command cmdlet?
body: <p>I have to get Event-Logs from some servers and I don't want to read in the credentials for each server which is found.</p>
  
  <p>I've tried to pass my variables by using the ArgumentList parameter but I doesn't work.</p>
  
  <p>This is my code:</p>
  
  <pre><code>$User = Read-Host -Prompt "Enter Username"
  $Password = Read-Host -Prompt "Enter Password" -AsSecureString
  $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
  $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
  
  Get-ADComputer -Filter "OperatingSystem -Like '*Server*'" | Sort-Object Name |
  ForEach-Object{
      if($_.Name -like '*2008*'){
          Invoke-Command -ComputerName $_.Name -ArgumentList $User, $UnsecurePassword -ScriptBlock {  
              net use P: \\Server\dir1\dir2 /persistent:no /user:$User $UnsecurePassword
              Get-EventLog -LogName System -After (Get-Date).AddHours(-12) -EntryType Error, Warning | format-list | 
              out-file P:\EventLog_$env:COMPUTERNAME.log
              net use P: /delete /yes
          }
      }
  }
  </code></pre>
  
  <p>How can I use the variables in the Invoke-Command ScriptBlock?</p>