我必须从某些服务器获取事件日志,并且我不想读取找到的每个服务器的凭据。我试图通过使用ArgumentList参数传递变量,但无法正常工作。这是我的代码:$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 } }}如何使用Invoke-Command ScriptBlock中的变量?
2 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
您可以在脚本块的开头声明参数:
{
param($user,$unsecurepassword)
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
}
或者,您使用$args变量访问参数:
#first passed parameter
$args[0]
#second passed parameter
$args[1]
....
添加回答
举报
0/150
提交
取消