Home > Mobile >  how do you turn an output string into something you can reference with get-itempropertyvalue?
how do you turn an output string into something you can reference with get-itempropertyvalue?

Time:10-25

I'm relatively new to Powershell and stackoverflow so sorry for mistakes. I have been setting myself things to work on like this so any help is greatly appreciated.

So I'm trying to write a script that compares the content of a source folder that only admins would have access to (containing .ps1 scripts) and a folder that users have access to (contains shortcuts to run .ps1 scripts that point to the source folder)

Anything that shows in the source folder without a correlating file in the shortcut folder is removed, anything showing in the source folder but not the shortcut folder a new shortcut is created in the shortcut folder (I have absolutely no idea how to make a shortcut in powershell yet, let alone how to get into advanced options and set it as run as admin)

I'm working on the beginning of this script trying to get the file names that are missing in a useable format

Right now This is what I have

$sourcelist = Get-ChildItem -path "C:\Users\yo\Documents\Powershell Lab\dump" | Get-ItemPropertyvalue -name "name"
$shortcutlist = Get-ChildItem -path "C:\Users\yo\Documents\Powershell Lab\fixes" | Get-ItemPropertyvalue -name "name"

$dif = Compare-Object -ReferenceObject $sourcelist -DifferenceObject $shortcutlist

$arr = $dif -split "=" -split ";"
$arr[1]
$arr[6]
$arr[11]

This outputs

abc.txt
find.ps1
hello!.txt

(still don't know how I would then split up the different files to be able to reference them to make a shortcut ill work on that later)

This method of using -split does what I need (returns the file names that is missing from $shortcutlist) however it requires me to repeat $arr[x] for as many files as it might find (could be allot)

So far the $dif outputs this

InputObject SideIndicator
----------- ------------- 
abc.txt     <=            
find.ps1    <=            
hello!.txt  <=   

Trying

Compare-Object -ReferenceObject $sourcelist -DifferenceObject
$shortcutlist | get-itemproperty -name "inputobject"

outputs

Cannot find path 'C:\windows\system32\@{InputObject=abc.txt; SideIndicator=<=}' because it does not exist.

Even if this worked I'm unsure how I would then select each file (that's an issue for another time)

One potential thought I had for fixing it was to turn what I believe is the string output of $dif into an object? really don't know anything about this yet so wasn't sure if the output was even what you call a string.

Anyway Thanks for your time and any information is appreciated, If you would do it a completely different way please let me know :)

Eventually id like it to modify it to be able to check the content of files within a folder ect but for now I'm just working on this.

CodePudding user response:

Instead of using -split, use the Where-Object cmdlet to filter the output from Compare-Object based on the value of the SideIndicator, then use ForEach-Object to grab only the value of the InputObject property on each resulting object:

$sourceNoShortcutList = Compare-Object -ReferenceObject $sourcelist -DifferenceObject $shortcutlist |Where-Object SideIndicator -eq '<=' |ForEach-Object InputObject

Even if this worked I'm unsure how I would then select each file (that's an issue for another time)

Loop over the resulting array:

foreach($filename in $sourceNoShortcutList){
  $filePath = Join-Path "C:\Users\yo\Documents\Powershell Lab\dump" $fileName 
  Remove-Item -LiteralPath $filePath
}
  • Related