我想在遠程計算機上新創建的檔案夾中安裝選定的程式和驅動程式。不幸的是它不能正常作業......有誰知道這可能是什么或對我有什么提示我對Powershell比較陌生。謝謝你的回答
$handler_submitwindow2_Click=
{
$Computer = Read-Host -Prompt 'Enter the Computer Name you are accessing'
New-PSSession -ComputerName $Computer
#----------Install Software On PC----------#
New-Item -ItemType directory -Path ".\%systemroot%\Temp\FileTransfer"
New-Item -ItemType directory -Path ".\%systemroot%\Temp\DriverInstallation"
New-Item -ItemType directory -Path ".\%systemroot%\Temp\SoftwareInstallation"
Copy-Item $openFileDialog1 ".\%systemroot%\Temp\FileTransfer" -Recurse
Copy-Item $openFileDialog2 ".\%systemroot%\Temp\DriverTransfer" -Recurse
Copy-Item $OpenFileDialog3 ".\%systemroot%\Temp\SoftwareInstallation" -Recurse
Write-Host "Software and Drivers get installed on $Computer"
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process $openFileDialog2 -ArgumentList "/q" -Wait}
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process $OpenFileDialog3 -ArgumentList "/q" -Wait}
}
uj5u.com熱心網友回復:
這里有兩個錯誤。
您正在使用.\表示當前目錄的運算子。然后您嘗試使用環境變數%systemroot%,它將決議為:
C:\Your\Current\Directory\C:\Windows\Temp\FileTransfer
這是錯誤的。如果您不使用當前目錄中的相對路徑,則應洗掉該運算子。
此外,正如評論中提到的@Theo,%systemroot%在 PowerShell 中不起作用。您必須改為使用語法$env:SystemRoot。
因此,您的最終腳本將如下所示:
$handler_submitwindow2_Click=
{
$Computer = Read-Host -Prompt 'Enter the Computer Name you are accessing'
New-PSSession -ComputerName $Computer
#----------Install Software On PC----------#
New-Item -ItemType directory -Path "$env:SystemRoot\Temp\FileTransfer"
New-Item -ItemType directory -Path "$env:SystemRoot\Temp\DriverInstallation"
New-Item -ItemType directory -Path "$env:SystemRoot\Temp\SoftwareInstallation"
Copy-Item $openFileDialog1 "$env:SystemRoot\Temp\FileTransfer" -Recurse
Copy-Item $openFileDialog2 "$env:SystemRoot\Temp\DriverTransfer" -Recurse
Copy-Item $OpenFileDialog3 "$env:SystemRoot\Temp\SoftwareInstallation" -Recurse
Write-Host "Software and Drivers get installed on $Computer"
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process $openFileDialog2 -ArgumentList "/q" -Wait}
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process $OpenFileDialog3 -ArgumentList "/q" -Wait}
}
uj5u.com熱心網友回復:
以下是針對您的腳本的更多建議:
New-PSSession $computer創建到另一臺計算機的連接,但不會導致以下命令在那里運行。您可能希望在腳本中使用類似Invoke-Command或Enter-PSSession類似的內容。可以使用
Admin$Windows 上的默認共享遠程復制您需要的檔案和檔案夾,該共享鏈接到系統目錄:
# Create folders
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\FileTransfer"
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\DriverInstallation"
New-Item -ItemType directory -Path "\\$Computer\Admin$\Temp\SoftwareInstallation"
# Copy installation files
Copy-Item $openFileDialog1 "\\$Computer\Admin$\Temp\FileTransfer" -Recurse
Copy-Item $openFileDialog2 "\\$Computer\Admin$\Temp\DriverTransfer" -Recurse
Copy-Item $OpenFileDialog3 "\\$Computer\Admin$\Temp\SoftwareInstallation" -Recurse
- 如果你想使用你的本地變數,比如
$openFileDialog1When usingInvoke-Command,你必須指定Using:范圍來告訴 powershell 你想要本地變數而不是來自遠程機器的東西:
Write-Host "Installing Software and Drivers on $Computer"
Invoke-Command -ComputerName $Computer -ScriptBlock {
Start-Process "$env:SystemRoot\Temp\DriverTransfer\$Using:openFileDialog2" -ArgumentList "/q" -Wait
Start-Process "$env:SystemRoot\Temp\SoftwareInstallation\$Using:OpenFileDialog3" -ArgumentList "/q" -Wait
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/351183.html
標籤:电源外壳
