PowerShell的传递日期时间变量设定日期
问题描述:
这里是代码:PowerShell的传递日期时间变量设定日期
$computerList = "localhost", "luna"
$user = Get-Credential powershell
$date = Get-Date
foreach ($computer in $computerList) {
if ($computer -eq "localhost") { $session = New-PSSession -ComputerName $computer }
else { $session = New-PSSession -ComputerName $computer -Credential $user }
Invoke-Command -Session $session -ScriptBlock {
Set-Date -Date $date
}
}
我得到这个错误:
Cannot bind argument to parameter 'Date' because it is null.
通过它的工作原理线console线工作,但运行脚本变量$时日期为空,就像错误说的那样。 为什么变量$ date为空?
答
它不应该工作无论哪种方式,在远程会话中执行命令时,你指的是局部变量,你应该调用这样的变量:
Invoke-Command -Session $session -ScriptBlock {
Set-Date -Date $using:date
}
太棒了!它现在有效。 – Overkill