【问题标题】:Passing a variable to powershell on remote machine将变量传递给远程机器上的powershell
【发布时间】:2018-02-16 05:13:03
【问题描述】:

我在这里遇到了一个奇怪的问题。基本上我需要删除远程服务器上符合某些条件的用户的配置文件文件夹。我不想删除服务器上的实际用户配置文件(注册表项会自动删除,配置文件文件夹应该是但偶尔会卡住)所以 wmi 类不起作用,我只需要删除文件夹。

我已经彻底尝试过 get-childitem | remove-item 但总是存在权限问题。我唯一知道每次都会起作用的是 rmdir /s /q ,它涉及到一个命令提示符。我已经尝试了几种方法,但到目前为止,我认为我最接近的方法是在下面,我挂断的是试图将 $Directory 变量传递给命令提示符。我什至曾经深入研究过系统变量,但这确实行不通,如果可能的话,我宁愿避免使用它。

    Function Profile-Cleanup {
    $Session = New-PSSession -ComputerName $Computer
    Enter-PSSession $Session

    Invoke-Command -Session $Session -ScriptBlock {
        cd c:\Users\
        $Directories = Get-ChildItem ("c:\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "b*") -and ($_.Name -notlike "C*")}
        ForEach($Directory in $Directories){

           Start-Process cmd -ArgumentList "rmdir /q /s" $Directory.Name ,"Exit" -Wait -Verb RunAs -PassThru

        }

    }
    Exit-PSSession
    Get-PSSession | Remove-PSSession 
    }

好的,谢谢@gms0ulman 将完整命令绑定到变量是我需要的技巧。也谢谢@postanote,虽然我没有在这个脚本块中使用它,但是一旦我可以解决它,我将传递参数,但现在这是我的工作代码,供其他遇到此问题的人使用。

Function Profile-Cleanup {
$Session = New-PSSession -ComputerName $Computer
$actiongood = $True
Try {
    Invoke-Command -Session $Session -ScriptBlock {
        cd c:\Users\
        $Computer = $env:COMPUTERNAME
        $date = (get-date).ToString('MM-dd-yyyy')
        $Directories = Get-ChildItem ("c:\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.LastWriteTime -lt $date) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "b") -and ($_.Name -notlike "c")}
        ForEach($Directory in $Directories){            
        $process = "/c rmdir /q /s " + $Directory.Name
        Start-Process cmd -ArgumentList $process, "Exit" -Wait -Verb RunAs
        }
    } -ErrorAction Stop

}Catch {
    Write-Host "Unable to process Profile Cleanup on computer " $Computer " ...sorry" -ForegroundColor Red
    $actiongood = $False
}
Get-PSSession | Remove-PSSession    
If ($actiongood){
    $FailedDirectories = Get-ChildItem ("\\"+ $Computer + "\C$\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.LastWriteTime -lt $date) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "n") -and ($_.Name -notlike "c")}
    Write-Host "The following are the user profiles were unable to be fully deleted on " $Computer " likely because they were in use" -ForegroundColor Green
    Write-Host "------------------------------------------------------------------" -ForegroundColor Green
    Write-Host $FailedDirectories
    Write-Host "------------------------------------------------------------------" -ForegroundColor Green
    Write-Host "Starting Next Process" -ForegroundColor Blue -BackgroundColor Black
    Write-Host "..." -ForegroundColor Blue -BackgroundColor Black
}

}

【问题讨论】:

  • 这与必须使用字符串或数组来传递变量有关 - 找不到源。如果您使用变量来存储第一部分 - $v = "rmdir /q /s" + $Directory.Name - 然后稍后使用 - -ArgumentList $v ,"Exit"
  • 实际上我认为这可能已经完成了......一旦我得到完全确认,我将发布更新的代码。
  • 做得很好。考虑发布解决方案编辑as an answeraccepting it

标签: powershell powershell-remoting


【解决方案1】:

至于你的主要目标.....

将变量传递给远程机器上的 powershell

...这实际上取决于您使用的 PoSH 版本。以后的版本允许使用以下...

使用远程变量

Windows PowerShell 假定远程命令中使用的变量 在命令运行的会话中定义。

在以下示例中,$ps 变量在临时文件中定义 Get-WinEvent 命令在其中运行的会话。

Invoke-Command -ComputerName S1 -ScriptBlock {$ps = "Windows PowerShell"; Get-WinEvent -LogName $ps}

同样,当命令在持久会话(PSSession)中运行时, 远程变量必须在同一个 PSSession 中定义。

使用局部变量

你也可以在远程命令中使用局部变量,但是你必须 表示该变量是在本地会话中定义的。

从 Windows PowerShell 3.0 开始,您可以使用 Using 范围 修饰符来标识远程命令中的局部变量。

Using的语法如下:

语法是:$Using:

在下面的例子中,$ps 变量是在本地创建的 会话,但在命令运行的会话中使用。这 使用范围修饰符将 $ps 标识为局部变量。

在 WINDOWS POWERSHELL 2.0 中使用本地变量

您可以通过定义参数在远程命令中使用局部变量 对于远程命令并使用 ArgumentList 参数 Invoke-Command cmdlet 将局部变量指定为参数 价值。

此命令格式在 Windows PowerShell 2.0 及更高版本上有效 Windows PowerShell 的版本。

-- 使用 param 关键字定义远程命令的参数。参数名称是不需要匹配的占位符 局部变量的名称。

-- 使用命令中param关键字定义的参数。

-- 使用 Invoke-Command cmdlet 的 ArgumentList 参数将局部变量指定为参数值。

例如,以下命令在 本地会话,然后在远程命令中使用它。该命令使用 $log 作为参数名称,局部变量 $ps 作为其值。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-5.1

【讨论】:

  • 嗯,这有助于让我更接近我认为。问题不仅仅是将变量传递给远程会话,这不一定是必需的,因为在我发布的代码中,我在 Invoke-Command 块中获取了变量 $Directory"。我遇到的问题是将变量传递给Start-Process cmd,命令提示符永远不会选择变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2015-01-14
  • 2023-01-02
  • 1970-01-01
相关资源
最近更新 更多