gpt4 book ai didi

azure - 使用 ARM 模板配置 Azure VM 时添加本地 AD 组 -Azure 虚拟桌面

转载 作者:行者123 更新时间:2023-12-03 03:40:12 27 4
gpt4 key购买 nike

我需要使用 ARM 模板配置 Azure VM,其中包括创建计算机、添加域加入、注册主机池、启用 Azure 磁盘加密。我们将使用图像。我最后尝试使用自定义扩展脚本来运行 ps1,它可以将机器对象添加到广告组。

脚本1

$SysInfo = New-Object -ComObject "ADSystemInfo"
$ComputerDN = $SysInfo.GetType().InvokeMember("ComputerName",
"GetProperty", $Null, $SysInfo,
$Null)
#$ComputerDN =
([ADSISEARCHER]"sAMAccountName=$($env:COMPUTERNAME)$").FindOne().Path
$ComputerDN
$Group = "groupname"
$group1dn= ([ADSISEARCHER]"sAMAccountName=$($Group)").FindOne().Path
$Groupdn = [ADSI]"$group1dn"

// Check if computer already a member of the group.
If ($Groupdn.IsMember("LDAP://$ComputerDN") -eq $False)
{
# Add the computer to the group.
$Groupdn.Add("LDAP://$ComputerDN")
}

脚本2

$credential= "domain/user & password"
Start-Process
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Credential
$credential -ArgumentList "-file <path of script1>"
**OR**
Invoke-Command -FilePath <path of script1>-Credential $credential -
ComputerName localhost

两个ps1都通过CSE下载到机器并触发第二个脚本2

对于启动过程,它显示访问被拒绝(因为 CSE 运行系统帐户并且可能无法更改域用户。)Invoke 命令可以模拟,但是,它需要将域/用户添加到 localadmin 用户组并在计算机上启用 psremoting,尽管这样做仍然存在问题。

Exception calling "InvokeMember" with "5" argument(s): "Access is denied.

The following exception occurred while retrieving member "IsMember": "An operations error occurred."

如何使用 CSE 完成此任务?

最佳答案

为了稍微简化 script1,您可以尝试使用 WinNT 名称并跳过搜索 AD:

$Group = "Domain Computers"
$Domain = 'CONTOSO' # NETBIOS name
$ADGroup = [ADSI]"WinNT://$Domain/$Group"

# Check if computer already a member of the group. Computer accounts get $ at the end
If (-not ($ADGroup.isMember("WinNT://$Domain/$env:COMPUTERNAME$"))) {
# Add the computer to the group.
$Groupdn.Add("LDAP://$ComputerDN")
}
只要虚拟机已加入域,并且凭据用户名采用 DOMAIN\user 格式,

script2 的 Start-Process 就应该没问题。例如,您可以尝试测试下面的命令 - 没有变量,只需填写名称。

获取已启动进程的输出可能会很痛苦。我添加了 ;Read-Host 'waiting' 来测试 shell 窗口中的 true/false 输出。如果您无法以交互方式运行它,您可以使用 -RedirectStandardOutput 'c:\folder\result.out' 并检查文件

$Credential = Get-Credential CONTOSO\user
Start-Process powershell.exe -Cred $Credential -Wait -Arg `
"-C ([ADSI]'WinNT://CONTOSO/Domain Computers').isMember('WinNT://CONTOSO/MyHostname$')"

在我的电脑上,这会返回错误start-process:访问被拒绝,但确实成功地模拟了我的域用户并启动了该进程...所以我不太确定它的作用是。

<小时/>

Invoke-Command 适用于计算机本地的任何内容,它仍然算作远程处理。由于第二跳限制,isMember() 无法对您的 AD 服务进行身份验证。这种行为can be changed ,但不建议这样做。

关于azure - 使用 ARM 模板配置 Azure VM 时添加本地 AD 组 -Azure 虚拟桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71524081/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com