Powershell、远程作业和特殊文件夹访问
Powershell, Remote Job and special folder access
我正在处理一些远程处理的事情,我似乎对远程机器上的环境变量有疑问。我用来启动远程作业的代码是
Invoke-Command -sessionOption:(New-PSSessionOption -noMachineProfile) -computerName:$machine -argumentList: $filePath –scriptblock {
param (
[String]$filePath
)
& powershell.exe -noProfile -executionpolicy bypass -file $filePath
} -credential:$credential -authentication:CredSSP –asJob -jobName:$machine > $null
而我 运行 远程的代码是
$fileName = "$([Environment]::UserName).log"
$desktop = [Environment]::GetFolderPath("Desktop")
Write-Host "$desktop$fileName!"
现在,据我了解,最后一段代码实际上是 运行 在远程计算机上,在 $credentials 中的用户凭据上下文中。我希望这会导致返回用户桌面上虚拟日志文件的完整路径。但是,在远程机器上 [Environment]::UserName 工作正常,但 [Environment]::GetFolderPath("Desktop") returns 什么都没有。我尝试了所有不同的方法,将变量赋值用引号括起来,如 $fileName,并在 Write-Host 中内联等。所有解决方案都有效 "locally" 但 none 在远程机器上有效,但是只有桌面是一个问题。我这个远程工作的用例将需要访问很多用户特殊文件夹,所以我希望这里有一些基本的东西是我遗漏的。否则我想我可以用字符串中内联的 $([Environment]::UserName) 硬编码所有路径,但是当 PS 提供内置访问时不是很优雅。
编辑:Hrm,另一个似乎在本地运行良好但在远程运行不正常的功能是 Balloon Tips。这是我的例子
[void[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloonTip = New-Object System.Windows.Forms.NotifyIcon
$balloonTip.icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloonTip.balloonTipIcon = "Info"
$balloonTip.balloonTipText = "Testing, one, two"
$balloonTip.balloonTipTitle = "$([Environment]::UserName)"
$balloonTip.visible = $true
$balloonTip.showBalloonTip(0)
不使用-NoMachineProfile
:
-NoMachineProfile <SwitchParameter>
Prevents loading the user's Windows user profile. As a result,
the session might be created faster, but user-specific registry
settings, items such as environment variables, and certificates
are not available in the session.
您的命令不会 运行 在交互式用户会话中(尝试 [Environment]::UserInteractive
),因此命令无法通过显示对话框 windows 或气球提示与会话交互。如果你想交互式地执行一些命令,那么你可以使用任务调度程序:
Register-ScheduledTask -TaskName NotepadTask `
-Action (New-ScheduledTaskAction -Execute Notepad) `
-Principal (New-ScheduledTaskPrincipal -UserId TestUser -LogonType Interactive)
Start-ScheduledTask -TaskName NotepadTask
我正在处理一些远程处理的事情,我似乎对远程机器上的环境变量有疑问。我用来启动远程作业的代码是
Invoke-Command -sessionOption:(New-PSSessionOption -noMachineProfile) -computerName:$machine -argumentList: $filePath –scriptblock {
param (
[String]$filePath
)
& powershell.exe -noProfile -executionpolicy bypass -file $filePath
} -credential:$credential -authentication:CredSSP –asJob -jobName:$machine > $null
而我 运行 远程的代码是
$fileName = "$([Environment]::UserName).log"
$desktop = [Environment]::GetFolderPath("Desktop")
Write-Host "$desktop$fileName!"
现在,据我了解,最后一段代码实际上是 运行 在远程计算机上,在 $credentials 中的用户凭据上下文中。我希望这会导致返回用户桌面上虚拟日志文件的完整路径。但是,在远程机器上 [Environment]::UserName 工作正常,但 [Environment]::GetFolderPath("Desktop") returns 什么都没有。我尝试了所有不同的方法,将变量赋值用引号括起来,如 $fileName,并在 Write-Host 中内联等。所有解决方案都有效 "locally" 但 none 在远程机器上有效,但是只有桌面是一个问题。我这个远程工作的用例将需要访问很多用户特殊文件夹,所以我希望这里有一些基本的东西是我遗漏的。否则我想我可以用字符串中内联的 $([Environment]::UserName) 硬编码所有路径,但是当 PS 提供内置访问时不是很优雅。
编辑:Hrm,另一个似乎在本地运行良好但在远程运行不正常的功能是 Balloon Tips。这是我的例子
[void[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloonTip = New-Object System.Windows.Forms.NotifyIcon
$balloonTip.icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloonTip.balloonTipIcon = "Info"
$balloonTip.balloonTipText = "Testing, one, two"
$balloonTip.balloonTipTitle = "$([Environment]::UserName)"
$balloonTip.visible = $true
$balloonTip.showBalloonTip(0)
不使用-NoMachineProfile
:
-NoMachineProfile <SwitchParameter>
Prevents loading the user's Windows user profile. As a result,
the session might be created faster, but user-specific registry
settings, items such as environment variables, and certificates
are not available in the session.
您的命令不会 运行 在交互式用户会话中(尝试 [Environment]::UserInteractive
),因此命令无法通过显示对话框 windows 或气球提示与会话交互。如果你想交互式地执行一些命令,那么你可以使用任务调度程序:
Register-ScheduledTask -TaskName NotepadTask `
-Action (New-ScheduledTaskAction -Execute Notepad) `
-Principal (New-ScheduledTaskPrincipal -UserId TestUser -LogonType Interactive)
Start-ScheduledTask -TaskName NotepadTask