如何在 Powershell 中正确组合函数
How to Properly Put together a Function in Powershell
我对如何使用 PSSession
将此 EXE 推送到远程 PC 进行了大量研究,并且所有代码行在逐行执行时都能正常工作。但是我很难将其放入一个有意义的函数中,并且将执行所有代码行并通过按一下按钮成功安装软件。不确定发生了什么。当我尝试将所有代码行放入函数中并 运行 时,它将在本地安装 exe。你能帮忙指导我做错了什么吗?抱歉,我是 Powershell 的新手。
$dc1 = New-PSSession -ComputerName DC1
Copy-Item C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe -Destination C:\TPAdmin -ToSession $dc1
Enter-PSSession -Session $dc1
Invoke-Command -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"
Remove-Pssession $dc1
至于……
Sorry i am a newbie at Powershell.
... 没关系,因为我们都必须从某个地方开始。但是...这里有几件事:
请务必格式化您的 post,以鼓励人们想要
帮助。人们不赞成不这样做。必须复制、粘贴和
重新格式化你的 post 很好,额外的不必要的工作。 ;-}。我们都去过那里。
我们不知道您是如何快速掌握 PowerShell 的,但是
使用免费资源 limit/avoid 所有
误解、挫折、错误、潜在的坏习惯等,
你将要遇到的。继续观看视频:
- Microsoft Virtual Academy
- MSDN Channel9
- Microsoft Learn
- 以及 reference
- 和 eBook 资源。
回到您的用例。你不说发生了什么。所以,你让我们去猜测。这对你并没有真正的潜在帮助。
尽管如此,您应该只需要这样做...在 PowerShell v5x 中,因为它需要使用 -ToSession 参数。
$DC1 = New-PSSession -ComputerName 'DC1'
Copy-Item -ToSession $DC1 -Path 'C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe' -Destination 'C:\TPAdmin'
Invoke-Command -Session $DC1 -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"}
Remove-PSSession -Session $DC1
我不确定你为什么要执行 Enter-PSSsssion in the New-PSSession 命令,因为不需要它。它用于独立的交互式会话。
Explicit PSRemoting = Enter=PSSEssion
Implicit PSREmoting = New-PSSEssion
如果在通过会话进行复制时所有其他方法均对您无效,则只需使用正常的 UNC 方式从源复制到目标。
Copy-Item -Path 'C:\temp\Results.csv' -Destination "\$($DC1.Computername)\c$\temp"
Enter-PSSession
仅用于 interactive ,因此不适合在 函数中使用 .[1]
不使用 Enter-PSSession
,将您使用 New-Session
创建的会话传递给 Invoke-Command
命令的 -Session
参数,这将 运行 该(远程)会话上下文中的命令。
# Define your function...
function Invoke-InstallerRemotely {
param([string] $ComputerName)
$session = New-PSSession -ComputerName $ComputerName
Copy-Item C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe -Destination C:\TPAdmin -ToSession $session
# Do NOT use Enter-PSSession.
# Pass your session to Invoke-Command with -Session
Invoke-Command -Session $session -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"
Remove-PSSession $session
}
# ... then invoke it.
# Note that functions must be defined *before* they're called.
Invoke-InstallerRemotely -ComputerName DC1
[1] 在函数中使用它意味着进入目标计算机上的交互式会话,您必须退出 交互地(通过键入和提交 exit
或 Exit-PSSession
),然后再执行函数中的其余语句,再次 locally.
我对如何使用 PSSession
将此 EXE 推送到远程 PC 进行了大量研究,并且所有代码行在逐行执行时都能正常工作。但是我很难将其放入一个有意义的函数中,并且将执行所有代码行并通过按一下按钮成功安装软件。不确定发生了什么。当我尝试将所有代码行放入函数中并 运行 时,它将在本地安装 exe。你能帮忙指导我做错了什么吗?抱歉,我是 Powershell 的新手。
$dc1 = New-PSSession -ComputerName DC1
Copy-Item C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe -Destination C:\TPAdmin -ToSession $dc1
Enter-PSSession -Session $dc1
Invoke-Command -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"
Remove-Pssession $dc1
至于……
Sorry i am a newbie at Powershell.
... 没关系,因为我们都必须从某个地方开始。但是...这里有几件事:
请务必格式化您的 post,以鼓励人们想要 帮助。人们不赞成不这样做。必须复制、粘贴和 重新格式化你的 post 很好,额外的不必要的工作。 ;-}。我们都去过那里。
我们不知道您是如何快速掌握 PowerShell 的,但是 使用免费资源 limit/avoid 所有 误解、挫折、错误、潜在的坏习惯等, 你将要遇到的。继续观看视频:
- Microsoft Virtual Academy
- MSDN Channel9
- Microsoft Learn
- 以及 reference
- 和 eBook 资源。
回到您的用例。你不说发生了什么。所以,你让我们去猜测。这对你并没有真正的潜在帮助。
尽管如此,您应该只需要这样做...在 PowerShell v5x 中,因为它需要使用 -ToSession 参数。
$DC1 = New-PSSession -ComputerName 'DC1'
Copy-Item -ToSession $DC1 -Path 'C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe' -Destination 'C:\TPAdmin'
Invoke-Command -Session $DC1 -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"}
Remove-PSSession -Session $DC1
我不确定你为什么要执行 Enter-PSSsssion in the New-PSSession 命令,因为不需要它。它用于独立的交互式会话。
Explicit PSRemoting = Enter=PSSEssion
Implicit PSREmoting = New-PSSEssion
如果在通过会话进行复制时所有其他方法均对您无效,则只需使用正常的 UNC 方式从源复制到目标。
Copy-Item -Path 'C:\temp\Results.csv' -Destination "\$($DC1.Computername)\c$\temp"
Enter-PSSession
仅用于 interactive ,因此不适合在 函数中使用 .[1]
不使用 Enter-PSSession
,将您使用 New-Session
创建的会话传递给 Invoke-Command
命令的 -Session
参数,这将 运行 该(远程)会话上下文中的命令。
# Define your function...
function Invoke-InstallerRemotely {
param([string] $ComputerName)
$session = New-PSSession -ComputerName $ComputerName
Copy-Item C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe -Destination C:\TPAdmin -ToSession $session
# Do NOT use Enter-PSSession.
# Pass your session to Invoke-Command with -Session
Invoke-Command -Session $session -ScriptBlock {C:\TPAdmin\Greenshot-INSTALLER-1.2.10.6-RELEASE.exe /VERYSILENT /LOG="C:\SOFTWAREINSTALL.LOG"
Remove-PSSession $session
}
# ... then invoke it.
# Note that functions must be defined *before* they're called.
Invoke-InstallerRemotely -ComputerName DC1
[1] 在函数中使用它意味着进入目标计算机上的交互式会话,您必须退出 交互地(通过键入和提交 exit
或 Exit-PSSession
),然后再执行函数中的其余语句,再次 locally.