Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何使用Invoke命令cmdlet传递变量?_Powershell_Variables_Invoke Command - Fatal编程技术网

Powershell 如何使用Invoke命令cmdlet传递变量?

Powershell 如何使用Invoke命令cmdlet传递变量?,powershell,variables,invoke-command,Powershell,Variables,Invoke Command,我必须从一些服务器获取事件日志,我不想读取找到的每个服务器的凭据 我尝试使用ArgumentList参数传递变量,但没有成功 这是我的代码: $User = Read-Host -Prompt "Enter Username" $Password = Read-Host -Prompt "Enter Password" -AsSecureString $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Pas

我必须从一些服务器获取事件日志,我不想读取找到的每个服务器的凭据

我尝试使用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
        }
    }
}

如何使用调用命令ScriptBlock中的变量?

在ScriptBlock的开头声明参数:

   {  
        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]
....

文档:

或者,您可以使用
$Using:
范围。参见本节下的示例5

例如:

$servicesToSearchFor = "*"
Invoke-Command -ComputerName $computer -Credential (Get-Credential) -ScriptBlock { Get-Service $Using:servicesToSearchFor }

使用
$Using:
您不需要脚本块中的
-ArgumentList
参数和
param
块。

只需确保
param()
中的变量与通过
-ArgumentList
传递的变量匹配即可。当我的$computer一直显示为我的用户ID时,我很难了解到这一点。这比
param
等更好!非常感谢。这对我有用。