我一直在关注这个ARM模板来构建VM并通过Chocolatey部署应用程序:
https://github.com/Azure/azure-quickstart-templates/tree/master/visual-studio-dev-vm-chocolatey
尽管GitHub上的扩展显示为Linux扩展,而不是MS扩展(我改变了它),但我成功部署了VM。但是,用于安装Chocolatey的PowerShell自动化脚本失败。
长话短说,我发现它在Server 2012上运行得很好,但它在Server 2016上无法运行。
以下是PowerShell脚本:
param([Parameter(Mandatory=$true)][string]$chocoPackages)
cls
#New-Item "c:\jdchoco" -type Directory -force | Out-Null
#$LogFile = "c:\jdchoco\JDScript.log"
#$chocoPackages | Out-File $LogFile -Append
# Get username/password & machine name
$userName = "artifactInstaller"
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$password = $([System.Web.Security.Membership]::GeneratePassword(12,4))
$cn = [ADSI]"WinNT://$env:ComputerName"
# Create new user
$user = $cn.Create("User", $userName)
$user.SetPassword($password)
$user.SetInfo()
$user.description = "Choco artifact installer"
$user.SetInfo()
# Add user to the Administrators group
$group = [ADSI]"WinNT://$env:ComputerName/Administrators,group"
$group.add("WinNT://$env:ComputerName/$userName")
# Create pwd and new $creds for remoting
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("$env:COMPUTERNAME\$($username)", $secPassword)
# Ensure that current process can run scripts.
#"Enabling remoting" | Out-File $LogFile -Append
Enable-PSRemoting -Force -SkipNetworkProfileCheck
#"Changing ExecutionPolicy" | Out-File $LogFile -Append
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
# Install Choco
#"Installing Chocolatey" | Out-File $LogFile -Append
$sb = { iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) }
Invoke-Command -ScriptBlock $sb -ComputerName $env:COMPUTERNAME -Credential $credential | Out-Null
#"Disabling UAC" | Out-File $LogFile -Append
$sb = { Set-ItemProperty -path HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System -name EnableLua -value 0 }
Invoke-Command -ScriptBlock $sb -ComputerName $env:COMPUTERNAME -Credential $credential
#"Install each Chocolatey Package"
$chocoPackages.Split(";") | ForEach {
$command = "cinst " + $_ + " -y -force"
$command | Out-File $LogFile -Append
$sb = [scriptblock]::Create("$command")
# Use the current user profile
Invoke-Command -ScriptBlock $sb -ArgumentList $chocoPackages -ComputerName $env:COMPUTERNAME -Credential $credential | Out-Null
}
Disable-PSRemoting -Force
# Delete the artifactInstaller user
$cn.Delete("User", $userName)
# Delete the artifactInstaller user profile
gwmi win32_userprofile | where { $_.LocalPath -like "*$userName*" } | foreach { $_.Delete() }
如果我要打开PowerShell ISE并直接在Server 2016计算机上运行,我会收到以下错误提示:
[asdf] Connecting to remote server asdf failed with the following error message : Access is denied. For more information,
see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (asdf:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
[asdf] Connecting to remote server asdf failed with the following error message : Access is denied. For more information,
see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (asdf:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At line:48 char:25
+ $command | Out-File $LogFile -Append
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
[asdf] Connecting to remote server asdf failed with the following error message : Access is denied. For more information,
see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (asdf:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
'asdf'是机器的名称
我可以逐步运行代码,但它始终会失败:
Invoke-Command -ScriptBlock $sb -ComputerName $env:COMPUTERNAME -Credential $credential
我可以看到帐户被创建,密码被创建,它被分配了管理员权限,但我想某处它只是不喜欢传递给命令的凭据。为什么这适用于2012年而不是2016年,我不知道。我不是PowerShell大师,所以请各位帮忙。
答案 0 :(得分:1)
要回答我自己的问题,似乎我必须运行winrm quickconfig
,这会在注册表中启用以下密钥:
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"
-Name 'LocalAccountTokenFilterPolicy' -Value '1' -PropertyType 'DWord'
因此,我调整了脚本,以便在操作之前和之后包含以下代码段。
在:
# Enable LocalAccountTokenFilterPolicy
$LocalAccToken1 = Get-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"
If(!($LocalAccToken1.GetValue("LocalAccountTokenFilterPolicy") -eq 1)) {
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' `
-Name 'LocalAccountTokenFilterPolicy' -Value '1' -PropertyType 'DWord' -Force
}
之后(我删除了设置'以防可能源于它的邪恶活动):
# Disable LocalAccountTokenFilterPolicy
$LocalAccToken2 = Get-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"
If($LocalAccToken2.GetValue("LocalAccountTokenFilterPolicy") -eq 1) {
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' `
-Name 'LocalAccountTokenFilterPolicy' -Force
}
...我希望MS更新他们的模板。