![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Scope of objects in scripts I am trying to modify a script (http://www.terminal23.net/2007/04/po...rmissions.html) to create an array of custom objects prior to the function call so that I can store all the results an post process them. I can successfully create the array and the collection of objects within the function but the collection does not persist. Can anyone explain what I am doing wrong? I have tried it with a normal associative array and this persists OK. My code is below $co = @() function GetExplicits ($folders) { foreach ($i in $folders) { # get the access list for each folder $acllist = get-acl $i.fullname foreach ($x in $acllist.Access) { # check to see if inherited flag is set If ($x.IsInherited -eq $false) { # output the data $spacing = $true # create new object $Script ermsObj = New-Object system.object$permsObj | add-member -membertype noteproperty -name Identity -value $($x.IdentityReference.Value) $permsObj | add-member -membertype noteproperty -name Path -value $($i.fullname) $co += $permsObj } } } } $folderslist = Get-Item -path $strpath GetExplicits $folderslist # get details for the child items - if they are containers $folderslist = Get-ChildItem -path $strpath -recurse | where {$_.psIscontainer -eq $true} GetExplicits $folderslist |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Scope of objects in scripts > I can successfully create the array and the collection of objects within the Quote: > function but the collection does not persist. > > Can anyone explain what I am doing wrong? I have tried it with a normal > associative array and this persists OK. My code is below > > $co = @()... I've never thought of how the scope might come into play when dealing with new-object... I have to think about that one... As for fixing this, you don't need to create an array. So try this instead: ####Start#### $strpath="c:\demo" #$co = @() function GetExplicits ($folders) { foreach ($i in $folders) { # get the access list for each folder $acllist = get-acl $i.fullname foreach ($x in $acllist.Access) { # check to see if inherited flag is set If ($x.IsInherited -eq $false) { # output the data $spacing = $true #<--Not sure what you're trying to do here # create new object $permsObj = New-Object system.object $permsObj | ` add-member -membertype noteproperty -name Identity -value $($x.IdentityReference.Value) $permsObj | ` add-member -membertype noteproperty -name Path -value $($i.fullname) #$co += $permsObj } } } } $folderslist = Get-Item -path $strpath GetExplicits $folderslist # get details for the child items - if they are containers $folderslist = Get-ChildItem -path $strpath -recurse | where {$_.psIscontainer -eq $true} GetExplicits $folderslist $permsobj ####End#### |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Scope of objects in scripts It doesn't work, on my box, when the "if" stament is enabled. Here's a modified version function GetExplicits ($folders){ foreach ($i in $folders){ $acllist = get-acl $i.fullname foreach ($x in $acllist.Access){ #If (!$x.IsInherited){ $permsObj = New-Object system.object $permsObj | add-member -membertype noteproperty Identity $x.IdentityReference.Value $permsObj | add-member -membertype noteproperty Path $i.fullname $permsObj #} } } } $folderslist = Get-childItem c:\test1 -Recurse | where {$_.psiscontainer} | $co = GetExplicits $folderslist $co ## sample output: Identity Path -------- ---- BUILTIN\Administrators C:\test1\Copy of New Folder NT AUTHORITY\SYSTEM C:\test1\Copy of New Folder DOMAIN\ShayL C:\test1\Copy of New Folder CREATOR OWNER C:\test1\Copy of New Folder BUILTIN\Users C:\test1\Copy of New Folder BUILTIN\Users C:\test1\Copy of New Folder BUILTIN\Users C:\test1\Copy of New Folder BUILTIN\Administrators C:\test1\New Folder NT AUTHORITY\SYSTEM C:\test1\New Folder DOMAIN\ShayL C:\test1\New Folder CREATOR OWNER C:\test1\New Folder BUILTIN\Users C:\test1\New Folder BUILTIN\Users C:\test1\New Folder BUILTIN\Users C:\test1\New Folder P.S, Type 'help about_scope' to learn more on PowerShell's scoping rules. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > I am trying to modify a script > (http://www.terminal23.net/2007/04/po...ermissions.htm > l) to create an array of custom objects prior to the function call so > that I can store all the results an post process them. > > I can successfully create the array and the collection of objects > within the function but the collection does not persist. > > Can anyone explain what I am doing wrong? I have tried it with a > normal associative array and this persists OK. My code is below > > $co = @() > > function GetExplicits ($folders) > { > foreach ($i in $folders) > { > # get the access list for each folder > $acllist = get-acl $i.fullname > foreach ($x in $acllist.Access) > { > # check to see if inherited flag is set > If ($x.IsInherited -eq $false) > { > # output the data > $spacing = $true > # create new object > $Script ermsObj = New-Object system.object> $permsObj | add-member -membertype noteproperty -name Identity > -value > $($x.IdentityReference.Value) > $permsObj | add-member -membertype noteproperty -name Path -value > $($i.fullname) > $co += $permsObj > } > } > } > } > $folderslist = Get-Item -path $strpath > GetExplicits $folderslist > # get details for the child items - if they are containers > $folderslist = Get-ChildItem -path $strpath -recurse | where > {$_.psIscontainer -eq $true} > GetExplicits $folderslist |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Scope of objects in scripts When I say doesn't work , I mean that the "If" condition doesn't fire so no objects are generated. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > It doesn't work, on my box, when the "if" stament is enabled. Here's a > modified version > > function GetExplicits ($folders){ > > foreach ($i in $folders){ > > $acllist = get-acl $i.fullname > > foreach ($x in $acllist.Access){ > #If (!$x.IsInherited){ > $permsObj = New-Object system.object > $permsObj | add-member -membertype noteproperty Identity > $x.IdentityReference.Value > $permsObj | add-member -membertype noteproperty Path $i.fullname > $permsObj > #} > } > } > } > $folderslist = Get-childItem c:\test1 -Recurse | where > {$_.psiscontainer} | > $co = GetExplicits $folderslist > $co > ## sample output: > > Identity Path > -------- ---- > BUILTIN\Administrators C:\test1\Copy of New Folder > NT AUTHORITY\SYSTEM C:\test1\Copy of New Folder > DOMAIN\ShayL C:\test1\Copy of New Folder > CREATOR OWNER C:\test1\Copy of New Folder > BUILTIN\Users C:\test1\Copy of New Folder > BUILTIN\Users C:\test1\Copy of New Folder > BUILTIN\Users C:\test1\Copy of New Folder > BUILTIN\Administrators C:\test1\New Folder > NT AUTHORITY\SYSTEM C:\test1\New Folder > DOMAIN\ShayL C:\test1\New Folder > CREATOR OWNER C:\test1\New Folder > BUILTIN\Users C:\test1\New Folder > BUILTIN\Users C:\test1\New Folder > BUILTIN\Users C:\test1\New Folder > > P.S, Type 'help about_scope' to learn more on PowerShell's scoping > rules. > > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com Quote: >> I am trying to modify a script >> (http://www.terminal23.net/2007/04/po...permissions.ht >> m l) to create an array of custom objects prior to the function call >> so that I can store all the results an post process them. >> >> I can successfully create the array and the collection of objects >> within the function but the collection does not persist. >> >> Can anyone explain what I am doing wrong? I have tried it with a >> normal associative array and this persists OK. My code is below >> >> $co = @() >> >> function GetExplicits ($folders) >> { >> foreach ($i in $folders) >> { >> # get the access list for each folder >> $acllist = get-acl $i.fullname >> foreach ($x in $acllist.Access) >> { >> # check to see if inherited flag is set >> If ($x.IsInherited -eq $false) >> { >> # output the data >> $spacing = $true >> # create new object >> $Script ermsObj = New-Object system.object>> $permsObj | add-member -membertype noteproperty -name Identity >> -value >> $($x.IdentityReference.Value) >> $permsObj | add-member -membertype noteproperty -name Path -value >> $($i.fullname) >> $co += $permsObj >> } >> } >> } >> } >> $folderslist = Get-Item -path $strpath >> GetExplicits $folderslist >> # get details for the child items - if they are containers >> $folderslist = Get-ChildItem -path $strpath -recurse | where >> {$_.psIscontainer -eq $true} >> GetExplicits $folderslist |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Scope of objects in scripts Marco I tried that but had a few issues, it would not work with the backticks in place before add-member. Taking those out caused the function to only store the latest item processed it creates a new instance of the object each time. Am I missing the point here somewhere? In addition the $permsObj is empty outside of the function. The code I posted would store all the permissions for each call of the function but it would be empty on exit. The function is called twice to handle the parent path and it's children. If I dumped the object output at the end of the function I got what I wanted but in two sections. Does this make sense. I can add examples if required. Thanks |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Scope of objects in scripts Shay The reason for the 'if' is to remove any permissions that are inherited. It's possible that your test folder inherited all the permissions of the parent and hence no output. Your modified example produces the output I need for the children (in an object outside of the function) but I also need to include the parent path. This is where the issue lies. How do I combine the output of the two calls to the function $folderslist = Get-Item -path $strpath $co = GetExplicits $folderslist $folderslist = Get-childItem -path $strpath -Recurse | where {$_.psiscontainer} $co = GetExplicits $folderslist $co using a += on the second call results in an error as there is no add function on the object. Thanks Alastair "Shay Levi" wrote: Quote: > When I say doesn't work , I mean that the "If" condition doesn't fire so > no objects are generated. > > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com > Quote: > > It doesn't work, on my box, when the "if" stament is enabled. Here's a > > modified version > > > > function GetExplicits ($folders){ > > > > foreach ($i in $folders){ > > > > $acllist = get-acl $i.fullname > > > > foreach ($x in $acllist.Access){ > > #If (!$x.IsInherited){ > > $permsObj = New-Object system.object > > $permsObj | add-member -membertype noteproperty Identity > > $x.IdentityReference.Value > > $permsObj | add-member -membertype noteproperty Path $i.fullname > > $permsObj > > #} > > } > > } > > } > > $folderslist = Get-childItem c:\test1 -Recurse | where > > {$_.psiscontainer} | > > $co = GetExplicits $folderslist > > $co > > ## sample output: > > > > Identity Path > > -------- ---- > > BUILTIN\Administrators C:\test1\Copy of New Folder > > NT AUTHORITY\SYSTEM C:\test1\Copy of New Folder > > DOMAIN\ShayL C:\test1\Copy of New Folder > > CREATOR OWNER C:\test1\Copy of New Folder > > BUILTIN\Users C:\test1\Copy of New Folder > > BUILTIN\Users C:\test1\Copy of New Folder > > BUILTIN\Users C:\test1\Copy of New Folder > > BUILTIN\Administrators C:\test1\New Folder > > NT AUTHORITY\SYSTEM C:\test1\New Folder > > DOMAIN\ShayL C:\test1\New Folder > > CREATOR OWNER C:\test1\New Folder > > BUILTIN\Users C:\test1\New Folder > > BUILTIN\Users C:\test1\New Folder > > BUILTIN\Users C:\test1\New Folder > > > > P.S, Type 'help about_scope' to learn more on PowerShell's scoping > > rules. > > > > ----- > > Shay Levi > > $cript Fanatic > > http://scriptolog.blogspot.com Quote: > >> I am trying to modify a script > >> (http://www.terminal23.net/2007/04/po...permissions.ht > >> m l) to create an array of custom objects prior to the function call > >> so that I can store all the results an post process them. > >> > >> I can successfully create the array and the collection of objects > >> within the function but the collection does not persist. > >> > >> Can anyone explain what I am doing wrong? I have tried it with a > >> normal associative array and this persists OK. My code is below > >> > >> $co = @() > >> > >> function GetExplicits ($folders) > >> { > >> foreach ($i in $folders) > >> { > >> # get the access list for each folder > >> $acllist = get-acl $i.fullname > >> foreach ($x in $acllist.Access) > >> { > >> # check to see if inherited flag is set > >> If ($x.IsInherited -eq $false) > >> { > >> # output the data > >> $spacing = $true > >> # create new object > >> $Script ermsObj = New-Object system.object> >> $permsObj | add-member -membertype noteproperty -name Identity > >> -value > >> $($x.IdentityReference.Value) > >> $permsObj | add-member -membertype noteproperty -name Path -value > >> $($i.fullname) > >> $co += $permsObj > >> } > >> } > >> } > >> } > >> $folderslist = Get-Item -path $strpath > >> GetExplicits $folderslist > >> # get details for the child items - if they are containers > >> $folderslist = Get-ChildItem -path $strpath -recurse | where > >> {$_.psIscontainer -eq $true} > >> GetExplicits $folderslist > > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Scope of objects in scripts Try: $folderslist = Get-Item -path $strpath $co1 = GetExplicits $folderslist $folderslist = Get-childItem -path $strpath -Recurse | where {$_.psiscontainer} $co2 = GetExplicits $folderslist $co = $co1+$co2 $co ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Shay > > The reason for the 'if' is to remove any permissions that are > inherited. It's possible that your test folder inherited all the > permissions of the parent and hence no output. > > Your modified example produces the output I need for the children (in > an object outside of the function) but I also need to include the > parent path. This is where the issue lies. How do I combine the output > of the two calls to the function > > $folderslist = Get-Item -path $strpath $co = GetExplicits $folderslist > > $folderslist = Get-childItem -path $strpath -Recurse | where > {$_.psiscontainer} > $co = GetExplicits $folderslist > $co > using a += on the second call results in an error as there is no add > function on the object. > > Thanks > > Alastair > > "Shay Levi" wrote: > Quote: >> When I say doesn't work , I mean that the "If" condition doesn't fire >> so no objects are generated. >> >> ----- >> Shay Levi >> $cript Fanatic >> http://scriptolog.blogspot.com Quote: >>> It doesn't work, on my box, when the "if" stament is enabled. Here's >>> a modified version >>> >>> function GetExplicits ($folders){ >>> >>> foreach ($i in $folders){ >>> >>> $acllist = get-acl $i.fullname >>> >>> foreach ($x in $acllist.Access){ >>> #If (!$x.IsInherited){ >>> $permsObj = New-Object system.object >>> $permsObj | add-member -membertype noteproperty Identity >>> $x.IdentityReference.Value >>> $permsObj | add-member -membertype noteproperty Path $i.fullname >>> $permsObj >>> #} >>> } >>> } >>> } >>> $folderslist = Get-childItem c:\test1 -Recurse | where >>> {$_.psiscontainer} | >>> $co = GetExplicits $folderslist >>> $co >>> ## sample output: >>> Identity Path >>> -------- ---- >>> BUILTIN\Administrators C:\test1\Copy of New Folder >>> NT AUTHORITY\SYSTEM C:\test1\Copy of New Folder >>> DOMAIN\ShayL C:\test1\Copy of New Folder >>> CREATOR OWNER C:\test1\Copy of New Folder >>> BUILTIN\Users C:\test1\Copy of New Folder >>> BUILTIN\Users C:\test1\Copy of New Folder >>> BUILTIN\Users C:\test1\Copy of New Folder >>> BUILTIN\Administrators C:\test1\New Folder >>> NT AUTHORITY\SYSTEM C:\test1\New Folder >>> DOMAIN\ShayL C:\test1\New Folder >>> CREATOR OWNER C:\test1\New Folder >>> BUILTIN\Users C:\test1\New Folder >>> BUILTIN\Users C:\test1\New Folder >>> BUILTIN\Users C:\test1\New Folder >>> P.S, Type 'help about_scope' to learn more on PowerShell's scoping >>> rules. >>> >>> ----- >>> Shay Levi >>> $cript Fanatic >>> http://scriptolog.blogspot.com >>>> I am trying to modify a script >>>> (http://www.terminal23.net/2007/04/po...ng_permissions. >>>> ht m l) to create an array of custom objects prior to the function >>>> call so that I can store all the results an post process them. >>>> >>>> I can successfully create the array and the collection of objects >>>> within the function but the collection does not persist. >>>> >>>> Can anyone explain what I am doing wrong? I have tried it with a >>>> normal associative array and this persists OK. My code is below >>>> >>>> $co = @() >>>> >>>> function GetExplicits ($folders) >>>> { >>>> foreach ($i in $folders) >>>> { >>>> # get the access list for each folder >>>> $acllist = get-acl $i.fullname >>>> foreach ($x in $acllist.Access) >>>> { >>>> # check to see if inherited flag is set >>>> If ($x.IsInherited -eq $false) >>>> { >>>> # output the data >>>> $spacing = $true >>>> # create new object >>>> $Script ermsObj = New-Object system.object>>>> $permsObj | add-member -membertype noteproperty -name Identity >>>> -value >>>> $($x.IdentityReference.Value) >>>> $permsObj | add-member -membertype noteproperty -name Path -value >>>> $($i.fullname) >>>> $co += $permsObj >>>> } >>>> } >>>> } >>>> } >>>> $folderslist = Get-Item -path $strpath >>>> GetExplicits $folderslist >>>> # get details for the child items - if they are containers >>>> $folderslist = Get-ChildItem -path $strpath -recurse | where >>>> {$_.psIscontainer -eq $true} >>>> GetExplicits $folderslist |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Scope of objects in scripts Also, take a look at this example. It updates $co with all *.ps1 and *.txt files. $co=@() function updateScriptScopeObject($path,$ext){ dir $path $ext | foreach { $permsObj = New-Object system.object $permsObj | add-member -membertype noteproperty Name $_.name $permsObj | add-member -membertype noteproperty Path $_.fullname $script:co+=$permsObj } } updateScriptScopeObject c:\scripts *.ps1 updateScriptScopeObject c:\scripts *.txt $co ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Try: > > $folderslist = Get-Item -path $strpath $co1 = GetExplicits > $folderslist > > $folderslist = Get-childItem -path $strpath -Recurse | where > {$_.psiscontainer} > > $co2 = GetExplicits $folderslist > > $co = $co1+$co2 > $co > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com Quote: >> Shay >> >> The reason for the 'if' is to remove any permissions that are >> inherited. It's possible that your test folder inherited all the >> permissions of the parent and hence no output. >> >> Your modified example produces the output I need for the children (in >> an object outside of the function) but I also need to include the >> parent path. This is where the issue lies. How do I combine the >> output of the two calls to the function >> >> $folderslist = Get-Item -path $strpath $co = GetExplicits >> $folderslist >> >> $folderslist = Get-childItem -path $strpath -Recurse | where >> {$_.psiscontainer} >> $co = GetExplicits $folderslist >> $co >> using a += on the second call results in an error as there is no add >> function on the object. >> Thanks >> >> Alastair >> >> "Shay Levi" wrote: >> Quote: >>> When I say doesn't work , I mean that the "If" condition doesn't >>> fire so no objects are generated. >>> >>> ----- >>> Shay Levi >>> $cript Fanatic >>> http://scriptolog.blogspot.com >>>> It doesn't work, on my box, when the "if" stament is enabled. >>>> Here's a modified version >>>> >>>> function GetExplicits ($folders){ >>>> >>>> foreach ($i in $folders){ >>>> >>>> $acllist = get-acl $i.fullname >>>> >>>> foreach ($x in $acllist.Access){ >>>> #If (!$x.IsInherited){ >>>> $permsObj = New-Object system.object >>>> $permsObj | add-member -membertype noteproperty Identity >>>> $x.IdentityReference.Value >>>> $permsObj | add-member -membertype noteproperty Path $i.fullname >>>> $permsObj >>>> #} >>>> } >>>> } >>>> } >>>> $folderslist = Get-childItem c:\test1 -Recurse | where >>>> {$_.psiscontainer} | >>>> $co = GetExplicits $folderslist >>>> $co >>>> ## sample output: >>>> Identity Path >>>> -------- ---- >>>> BUILTIN\Administrators C:\test1\Copy of New Folder >>>> NT AUTHORITY\SYSTEM C:\test1\Copy of New Folder >>>> DOMAIN\ShayL C:\test1\Copy of New Folder >>>> CREATOR OWNER C:\test1\Copy of New Folder >>>> BUILTIN\Users C:\test1\Copy of New Folder >>>> BUILTIN\Users C:\test1\Copy of New Folder >>>> BUILTIN\Users C:\test1\Copy of New Folder >>>> BUILTIN\Administrators C:\test1\New Folder >>>> NT AUTHORITY\SYSTEM C:\test1\New Folder >>>> DOMAIN\ShayL C:\test1\New Folder >>>> CREATOR OWNER C:\test1\New Folder >>>> BUILTIN\Users C:\test1\New Folder >>>> BUILTIN\Users C:\test1\New Folder >>>> BUILTIN\Users C:\test1\New Folder >>>> P.S, Type 'help about_scope' to learn more on PowerShell's scoping >>>> rules. >>>> ----- >>>> Shay Levi >>>> $cript Fanatic >>>> http://scriptolog.blogspot.com >>>>> I am trying to modify a script >>>>> (http://www.terminal23.net/2007/04/po...ng_permissions >>>>> . ht m l) to create an array of custom objects prior to the >>>>> function call so that I can store all the results an post process >>>>> them. >>>>> >>>>> I can successfully create the array and the collection of objects >>>>> within the function but the collection does not persist. >>>>> >>>>> Can anyone explain what I am doing wrong? I have tried it with a >>>>> normal associative array and this persists OK. My code is below >>>>> >>>>> $co = @() >>>>> >>>>> function GetExplicits ($folders) >>>>> { >>>>> foreach ($i in $folders) >>>>> { >>>>> # get the access list for each folder >>>>> $acllist = get-acl $i.fullname >>>>> foreach ($x in $acllist.Access) >>>>> { >>>>> # check to see if inherited flag is set >>>>> If ($x.IsInherited -eq $false) >>>>> { >>>>> # output the data >>>>> $spacing = $true >>>>> # create new object >>>>> $Script ermsObj = New-Object system.object>>>>> $permsObj | add-member -membertype noteproperty -name Identity >>>>> -value >>>>> $($x.IdentityReference.Value) >>>>> $permsObj | add-member -membertype noteproperty -name Path -value >>>>> $($i.fullname) >>>>> $co += $permsObj >>>>> } >>>>> } >>>>> } >>>>> } >>>>> $folderslist = Get-Item -path $strpath >>>>> GetExplicits $folderslist >>>>> # get details for the child items - if they are containers >>>>> $folderslist = Get-ChildItem -path $strpath -recurse | where >>>>> {$_.psIscontainer -eq $true} >>>>> GetExplicits $folderslist |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Scope of objects in scripts "Shay Levi" wrote: Quote: > Also, take a look at this example. It updates $co with all *.ps1 and *.txt > files. > > $co=@() > > function updateScriptScopeObject($path,$ext){ > dir $path $ext | foreach { > $permsObj = New-Object system.object > $permsObj | add-member -membertype noteproperty Name $_.name > $permsObj | add-member -membertype noteproperty Path $_.fullname > $script:co+=$permsObj > } > } > > > > updateScriptScopeObject c:\scripts *.ps1 > updateScriptScopeObject c:\scripts *.txt > > $co > > > > other combinations just not that one. Thanks again Alastair French |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| run .cmd scripts on remote machine (automating the MOSS install via scripts) | PowerShell | |||
| Is this a bug in the scope? | PowerShell | |||
| Variable scope | PowerShell | |||
| Execute scripts in current scope? | PowerShell | |||
| Using a function to setup objects properties with scope of script | PowerShell | |||