Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

ForEach-Object - Test-Path und Select

$
0
0

Hallo, 

ich möchte, dass bevor der neue Ordner C:\Data\$OU1\$OU2\$_.name angelegt wird, auch getestet wird, ob der Ordner "$_.name" schon existiert und nur angelegt wird, wenn er nicht existiert. Mit Test-Path schaffe ich das nicht, weil ich ja nicht Test-Path -path "C:\Data\$OU\$OU1\$_.name" schreiben kann.

if ($Test.Count -gt 0) {
        $t=forEach ($_ in $Test) {
            Get-ADOrganizationalUnit -SearchBase $_.distinguishedName -SearchScope OneLevel -Filter *| Select Name, DistinguishedName | ForEach-Object {New-Item -path "C:\Data\$OU\$OU2" -Name $_.name -ItemType Directory -Force}}

Zudem möchte ich den DistinguishedName von $t in ein json File schreiben ($t.DistinguishedName). Im json File steht aber nur "null" beim DistinguishedName, obwohl es einen DistinguishedName gibt. Jedoch funktioniert es, wenn ich es so mache ($t1): (optimal wäre es aber wenn ich das gleich irgendwie verbinden könnte): 

$prod =forEach ($_ in $Test) {
            Get-ADOrganizationalUnit -SearchBase $_.distinguishedName -SearchScope OneLevel -Filter *| ForEach-Object {New-Item -path "C:\Data\$OU\$OU2" -Name $_.name -ItemType Directory -Force}}

        $t1= forEach ($_ in $Test) {
        @(Get-ADOrganizationalUnit -SearchBase $_.distinguishedName -SearchScope OneLevel -Filter *| Select Name, DistinguishedName)}
        
   

LG und DANKE! 

 



Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>