使用automation.runspaces命名空間我造成了很大的問題。我是一名學生,我在一所大學工作。我正在使用大學計算機和大學領域。長話短說,放棄使用運行空間,我只是在C#中打開PowerShell並執行腳本。這沒有奏效,所以我寫了一些cmd腳本。這裏是工作的例子:
PowerShell腳本:
重命名MAC地址
$OS = Get-WmiObject Win32_OperatingSystem
$NIC = Get-WmiObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq 「TRUE」}
$PC = Get-WmiObject -class Win32_ComputerSystem
if($NIC.MacAddress -eq $null)
{
Write-Host "No Network Connection! The PC will not be renamed until the network is enabled"
}
else
{
$MacAddress = $NIC.MacAddress
$NewPCName = "PC-" + $MacAddress.Substring(0) -replace ':'
$PC.Rename($NewPCName)
Write-Host -ForegroundColor Green "New PC Name"
$NewPCName
}
write-Host "The PC has been renamed and will restart in 5 seconds."
start-sleep -s 5
restart-computer
如果我運行上面的腳本在PowerShell中它的工作原理。如果我使用Process.Start(powershell.exe,「C:\ myscript」);在C#中它不會運行。即使設置執行策略也無濟於事。所以我用一個CMD腳本如下:
CMD腳本:
PowerShell的-noexit -ExecutionPolicy不受限制的 「C:\ myscript.ps1」
並執行。
現在我有以下幾點:
PowerShell腳本:
的Set-ItemProperty 「HKLM:\ SOFTWARE \微軟\的Windows NT \ CURRENTVERSION \ Winlogon中」 -name AutoAdminLogon - 值0 寫 - 主機「更改註冊表」 啓動睡眠-s 10
CMD腳本:
PowerShell的-noexit -ExecutionPolicy不受限制的 「C:\ WINDOWS \ ITS \ ChangeRegistry.ps1」
在我的Windows窗體程序中我有一個按鈕單擊事件調用cmd腳本。它看起來像它的工作,沒有崩潰,cmd執行,因爲它應該寫入主機,它睡了10秒。但註冊表項從不改變。但我的PowerShell重命名腳本和cmd的第一個例子完全適用於單擊窗體上的按鈕。所以我只是不明白。
來源
2012-07-28 21:41:24
Jon