i am creating a function which first checks the existence of these two GPOs in GPMC.msc and if they don't exist, it dowsn't generates error & instead it creates them.
function MyCreatADOUs {
[Array]$GPOs="GPO-OU-Users","GPO-OU-computers"
$GPOs | foreach-object {
try {
Get-GPO-name$_ }
catch {
New-GPO-name$_}
}
New-GPLink"GPO-OU-Users"-target'ou=ou-users,dc=hp,dc=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-computers"-target'ou=ou-computers,dc=hp,dc=lab'-LinkEnabled yes -ea Ignore
}
but i get these errors:
get-gpo : The "GPO-OU-Users" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At C:\Users\Administrator\Desktop\Untitled2.ps1:7 char:24
+ get-gpo -name $GPO }
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
ommand
get-gpo : The "GPO-OU-computers" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At C:\Users\Administrator\Desktop\Untitled2.ps1:7 char:24
+ get-gpo -name $GPO }
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
ommand
PS C:\> [Array]$GPOs = "GPO-OU-Users","GPO-OU-computers"
$GPOs | % {
try {
get-gpo -name $_ }
catch {
New-GPO -name $_}
}
New-GPLink "GPO-OU-Users" -target 'OU=OU-users,DC=HP,DC=lab' -LinkEnabled yes -ea Ignore
New-GPLink "GPO-OU-computers" -target 'OU=OU-computers,DC=HP,DC=lab' -LinkEnabled yes -ea Ignore
get-gpo : The "GPO-OU-Users" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At line:4 char:24
+ get-gpo -name $_ }
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
ommand
get-gpo : The "GPO-OU-computers" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At line:4 char:24
+ get-gpo -name $_ }
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
ommand
i also tested this code:
PS C:\> [Array]$GPOs="GPO-OU-Users","GPO-OU-computers"
$GPOs | % {
$GOP="$_"
try {
get-gpo-name$GPO }
catch {
New-GPO-name$GPO}
}
New-GPLink"GPO-OU-Users"-target'OU=OU-users,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-computers"-target'OU=OU-computers,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
$GPOs | % {
$GOP="$_"
try {
get-gpo-name$GPO }
catch {
New-GPO-name$GPO}
}
New-GPLink"GPO-OU-Users"-target'OU=OU-users,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-computers"-target'OU=OU-computers,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
but again this error:
New-GPLink"GPO-OU-Users"-target'OU=OU-users,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-computers"-target'OU=OU-computers,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-Users"-target'OU=OU-users,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
New-GPLink"GPO-OU-computers"-target'OU=OU-computers,DC=HP,DC=lab'-LinkEnabled yes -ea Ignore
get-gpo : The "GPO-OU-Users" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At line:5 char:24
+ get-gpo -name $GPO }
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
ommand
get-gpo : The "GPO-OU-computers" GPO was not found in the HP.lab domain.
Parameter name: gpoDisplayName
At line:5 char:24
+ get-gpo -name $GPO }
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.GroupPolicy.GPDomain:GPDomain)
[Get-GPO], ArgumentException
+ FullyQualifiedErrorId : GpoWithNameNotFound,Microsoft.GroupPolicy.Commands.GetGpoC
command
any help please
thanks in advanced