gpt4 book ai didi

validation - 在创建 OU 之前检查它是否存在

转载 作者:行者123 更新时间:2023-12-02 22:34:42 25 4
gpt4 key购买 nike

function NyChildOU {
$overOU = Read-Host "Type in the name of the parrent OU"
$oucheck = [adsi]::Exists("LDAP://OU=$overOU,OU=PS,DC=PS,DC=local")
if ($oucheck -eq "true") {
$navnpaaou = Read-Host "Type in the name of the new OU"
$oucheck2 = [adsi]::Exists("LDAP://OU=$navnpaaou,OU=$overOU,OU=PS,DC=PS,DC=local")
if ($oucheck2 -eq "false") {
New-ADOrganizationalUnit -Name $navnpaaou -path "OU=$navnpaaou,OU=$overOU,OU=PS,DC=PS,DC=Local"
Write-Host "The new entry: $navnpaaou is created within $overOU"
} else {
Write-Host "OUen $navnpaaou do exist within $overOU"
}
} else {
Write-Host "OUen $overOU doesen't exist, trie again"
}
}

这是我的脚本,其目的是创建一个 OU,除非它已经存在。我只是无法弄清楚我的代码有什么问题。

最佳答案

只需检查是否 Get-ADOrganizationalUnit返回具有该可分辨名称的 OU 并以其他方式创建它:

$parentOU = 'OU=parent,OU=PS,DC=example,DC=com'
$navnpaaou = Read-Host "Type in the name of the new OU"
$newOU = "OU=$navnpaaou,$parentOU"
if (Get-ADOrganizationalUnit -Filter "distinguishedName -eq '$newOU'") {
Write-Host "$newOU already exists."
} else {
New-ADOrganizationalUnit -Name $navnpaaou -Path $parentOU
}

关于validation - 在创建 OU 之前检查它是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33941460/

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