Honestly? Modify your original script to put this into a SQL Express database. One column for UNC, another for permission. Then querying a given UNC would be child’s play.
Otherwise you’re looking at a slow process of parsing that file, a memory-hungry (and slow) process of making a data structure in memory… why not just put it into a data structure to begin with?
Sure. You could also read our free ebook on the subject, “Creating Trend and Historical Reports.” But, briefly:
Suppose I have a SQL Express instance on my local machine, containing a database named MYDB and a table named MYTABLE. The table has a column named UNC and one named PRINCIPAL, both of which are strings.
Approximately. That’s top-of-head, not code I just tested. Once it’s all set up, it’s really just three lines to make each query and execute it. Database populated, easy to run reports (SQL Express can come with SQL Server Reporting Services), easy to make queries. Again - I did a whole ebook on this, and it isn’t terribly long.
I noticed your first example doesnt work, but i assume thats because the array ‘lines’ isnt comma deliminated.
Edit: not an array, but a here-string…still doesnt appear to be working but i’ll look at it.
I like the idea of an xml, but i’m not sure how to create an xml variable and save to it, i’ve only read from existing xml files before.
$shares = Get-WmiObject win32_share -ComputerName "vmibfilestore"
foreach($share in (($shares).name | Where-Object {$_ -eq "Marketing"}))
{
$permissions = get-childitem -Recurse "\\server\$($share)" | Where-Object{$_.PSIsContainer}
foreach($permission in $permissions)
{
$acls = $permission | Get-Acl
#split on :: as path shows as Microsoft.PowerShell.Core\FileSystem::\\server\whatever
$path = ($acls.path).split("::")[2]
$users = (@($acls.Access).identityreference).value
}
}
Is there an easy way to create an xml variable and save path>line for users under path?
The example should work as posted, you will likely need to adapt it for your data file. If you use get-content to read the file, it’s already an array of lines and you won’t need to split it.
For XML, I would just create an array of PSObjects and then export to XML. Then you can read those objects back in with a corresponding import on the other end.
Exception calling "ExecuteNonQuery" with "0" argument(s): "String or binary data would be truncated.
The statement has been terminated."
At line:1 char:1
+ $command.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
That error usually comes from your SQL statement. I’d check to make sure the field sizes are large enough - e.g., use VARCHAR(MAX) or something. It’s telling you that it couldn’t insert your data because doing so would have truncated some of it - meaning the field in the database is too small for the data.