Powershell权限和执行策略

在closures之前,请注意它不是一个代码问题,而是一个环境,特权和政策问题,我们如何在整个环境中实现这一点。

我们正在定期创buildVHD,服务器和客户端,很大一部分这个脚本是使用下面的batch file从一个提升的pipe理命令提示符下运行的。

该脚本不是一个问题,因为它在多个Win 8.1 Pro和Server计算机上运行良好。

对于Win 8.1机器和一些不工作的服务器。
我想弄清楚为什么并将环境设置,策略应用于所有机器。

最好是一种自动化的方式,或者如果没有select手动的话

它触发的错误消息:
New-VHD : The term 'New-VHD' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At E:\VHD\VhdCreateFormat\VhdCreateFormat.ps1:4 char:1 + New-VHD -Path $vhdpath -Dynamic -SizeBytes $vhdsize | Mount-VHD -Passthru | Init ... + ~~~~~~~ + CategoryInfo : ObjectNotFound: (New-VHD:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

代码供参考。 显然这个cmdlet是从Win 8.1 / 08 R2 / 12开始的,从我在网上阅读的那个版本开始。

VHD_CF.ps1
$vhdpath = "E:\VHD\NameItServerClient.vhd" $vhdsize = 25GB $vhdVolFsName = "NameItServerClient" New-VHD -Path $vhdpath -Dynamic -SizeBytes $vhdsize | Mount-VHD -Passthru | Initialize-Disk -PartitionStyle MBR -Passthru |New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -NewFileSystemLabel $vhdVolFsName -FileSystem NTFS -Confirm:$false -Force Write-Host "Press any key to continue ..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Write-Host "Press any key to continue ...2 " Function Pause($M="Press any key to continue . . . "){If($psISE){$S=New-Object -ComObject "WScript.Shell";$B=$S.Popup("Click OK to continue.",0,"Script Paused",0);Return};Write-Host -NoNewline $M;$I=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183;While($K.VirtualKeyCode -Eq $Null -Or $I -Contains $K.VirtualKeyCode){$K=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")};Write-Host} Write-Host "Press any key to continue ...3" $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

通过同名的batch file运行它:

VHD_CF.bat
REM So we don't have to re-write the batch file for every script, or every time we move a script around, it's going to make use of a self-referencing variable to build the file path for the PowerShell script. To make this work, the batch file will need to be placed in the same folder as your PowerShell script and have the same file name. So if your PowerShell script is called “MyScript.ps1”, you'll want to name your batch file “MyScript.bat” and make sure it's in the same folder. Then, put these lines in the batch script:

REM @ECHO OFF PowerShell.exe -NoProfile -Command“&{启动进程PowerShell.exe -ArgumentList' – 无configuration文件 – 执行策略Bypass – 文件”“%〜dpn0.ps1”“'-Verb RunAs}”暂停

New-Vhd是Hyper-V PowerShell cmdlet的一部分。 假设你在服务器本身远程调用这个命令,我会想象你的一些服务器没有安装这些cmdlet。