I am trying to take a variable, split it and then rebuild it so I can use Test-Path iteratively. I am not sure it is possible. The code I have so far in its basic form looks something like this, the trouble I have is around rebuilding the registry path.
$Key = "HKCU:\SOFTWARE\MyKey\Test"
$SplitKey = $Key -split "\\"
#Write-Host $SplitKey.Length
$i = 0
do {
if ($i -gt 0) {
$x = $i - 1
$sk = $SplitKey[$x] + "\" + $SplitKey[$i] + "\"
Write-Host $sk
} else {
$sk = $SplitKey[$i] + "\"
Write-Host $sk
}
$i++
} until ($i -ge $SplitKey.Length)
The first part of the key is rebuilt exactly how I want. My plan is to incorporate a Test-Path into the loop and where required do a New-Item if Test-Path fails.
Can anyone help with the loop and rebuilding the $SplitKey array step by step?
This is what causes your problem:
$x = $i - 1
$sk = $SplitKey[$x] + "\" + $SplitKey[$i] + "\"
You create $sk just from the previous and the current array element, which basically works like this:
Original string: HKCU:\SOFTWARE\MyKey\Test
1st iteration: HKCU:\
2nd iteration: HKCU:\SOFTWARE\
3rd iteration: SOFTWARE\MyKey\
4th iteration: MyKey\Test\
What you actually want is to concatenate the current array element to everything that came before, i.e. to the current value of $sk:
$sk += "\" + $SplitKey[$i]
Or you could join the array elements up to the current index:
$sk = $SplitKey[0..$i] -join '\'
With that said, personally I prefer recursive algorithms for path creation. Traverse from the full path up to the longest existing path, then create the missing folders when you descend back down as you return from the recursive calls:
function New-Key([string]$Path) {
$drive = Split-Path $Path -Qualifier
$parent = try { Split-Path $Path -NoQualifier | Split-Path -Parent } catch {}
if (-not (Test-Path -LiteralPath $Path)) {
New-Key (Join-Path $drive $parent)
New-Item -Type Directory -Path $Path
}
}
Related
Need help troubleshooting an the Array and Scriptblock
OR Maybe this is better using param and functions???
Script Objective: To easily update the list of applications to be installed
Getting error below.
'
At C:\Temp\appinstall.ps1:7 char:10
$Firefox={
~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept
assignments, such as a variable or a property.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : InvalidLeftHandSide
'
Start-Transcript -Append c:\Deploy\log.txt
$ProgressPreference = 'SilentlyContinue';
#Change App Name, Source, MSI/EXE, Argument
$AppArray= (
$Firefox={
$App= "Firefox";
$App_source= "https://download.mozilla.org/?product=firefox-latest&os=win64&lang=en-US";
$destination = "c:\Deploy\$App.exe";
$Argument= "/S";
},
$Chrome=
{
$App= "Chrome";
$App_source= "https://dl.google.com/tag/s/defaultbrowser/edgedl/chrome/install/GoogleChromeStandaloneEnterprise64.msi";
$destination = "c:\Deploy\$App.exe";
$Argument= "/norestart","/qn";
}
)
$InstallScriptBlock=
{
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -Match "$App" });
$installed.displayname
if ($installed.displayname -Match $App) {
Write-Host "$software installed"
}else{
If ((Test-Path $destination) -eq $false) {
New-Item -ItemType File -Path $destination -Force
}
#install software
Invoke-WebRequest $App_source -OutFile $destination
Start-Process -FilePath "$destination" -ArgumentList "$Argument" -Wait
#Delete installer
Remove-Item -recurse "$destination"
}
}
ForEach ($Program in $AppArray) {Invoke-Command -ScriptBlock $InstallScriptBlock}
Stop-Transcript
It looks like you're trying to create a nested hashtable (#{ ... }), but your syntax is flawed - see the linked docs.
However:
It should suffice in your case to create an array of hashtables to iterate over with foreach
There's no need to use a separate script block ({ ... }) - just use the body of the foreach loop statement.
As an aside: While using Invoke-Command for local invocation of script blocks works, it usually isn't necessary, because &, the call operator, will do (e.g. $sb = { 'hi' }; & $sb). Invoke-Command's primary purpose is to execute a script block on a remote machine.
Generally, you can use variables as-is as command arguments, without enclosing them in "..." - even if their values contain spaces. E.g., Write-Output $foo is sufficient, no need for Write-Output "$foo"
To put it all together:
# Create an array whose elements are hashtables.
$appArray = (
#{
App = ($thisApp = 'Firefox')
App_source = 'https://download.mozilla.org/?product=firefox-latest&os=win64&lang=en-US'
Destination = "c:\Deploy\$thisApp.exe"
Argument = '/S'
},
#{
App = ($thisApp = 'Chrome')
App_source = 'https://dl.google.com/tag/s/defaultbrowser/edgedl/chrome/install/GoogleChromeStandaloneEnterprise64.msi'
Destination = "c:\Deploy\$thisApp.exe"
Argument = '/norestart /qn'
}
)
foreach ($app in $appArray) {
# Note how $app.<key> is used to refer to the entries of the hashtable at hand,
# e.g. $app.App yields "Firefox" for the first hashtable.
$installed = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -Match $app.App }
$installed.displayname
if ($installed.displayname -Match $app.App) {
Write-Host "$($app.App) already installed."
}
else {
if ((Test-Path $app.Destination) -eq $false) {
New-Item -ItemType File -Path $app.Destination -Force
}
#install software
Invoke-WebRequest $app.App_source -OutFile $app.Destination
Start-Process -FilePath $app.Destination -ArgumentList $app.Argument -Wait
#Delete installer
Remove-Item -Recurse $app.Destination
}
}
Note:
I've removed unnecessary ; and I've switched to using verbatim (single-quoted) strings ('...') when no string interpolation via expandable (double-quoted) strings ("...") is required, both for conceptual clarity and to avoid potentially unwanted expansions.
Note the use of aux. variable $thisApp in the App key, which allows referencing it in the later Destination key, in an expandable string ("c:\Deploy\$thisApp.exe").
GitHub suggestion #13782 looks for a more elegant way to allow hashtable entries to reference one another.
I'm working on a script to compare two directories. There are two main things I want the script to show in the output--which files exist on one directory but not the other, and which files appear in both directories but have differences in them. Matching files don't need to show up.
I got some advice before on how to achieve this, but since I'm still pretty new to PS I'm having trouble executing it. What I'm trying to do is this:
I have Path #1. For each file in that path, I want to test for their existence on Path #2.
If the file exists in both paths, do a hash comparison between them. If there are differences, add the files to List A.
If the file appears in Path 1 but not Path 2, put them in List B.
This isn't as important, but would it also be possible to find files that exist in Path 2 but not Path 1? For work purposes that probably won't matter, but it will still be nice just in case.
Take the output and format it so that it can show something like: "The following files exist in Path 1 and not Path 2," and "The following files exist in both paths but have differences."
Basically, I don't just want an info dump of files to be the output and people end up having to puzzle through it. And like I said, I think the advice I received on how to do it will be good, I'm just having trouble making it work.
Here's the code I have so far:
$Source = #(Get-ChildItem -Recurse -Path \\SERVER\D$\PSTest)
foreach ($file in $Source){
If ($Target = Test-Path #(Get-ChildItem -Recurse -Path \\SERVER\D$\PSTest))
{
$HashResult = (Compare-Object -ReferenceObject $file -DifferenceObject
$Target -Property hash -PassThru).Path
}
else {
$Missing += $file
}
}
Write-Host 'These files have differences.' -ForegroundColor Green
$HashResult
Write-Host 'These files are missing from the target path.' -ForegroundColor
Green
$Missing
When I run that, I don't get any results (other than the text output). Where am I going wrong with this?
Made a few assumptions about the file names and their uniqueness down through the various depths of the source/target folders:
$SourceDir = "C:\\temptest";
$DestDir = "D:\\temptest";
$SourceFiles = #(Get-ChildItem -Recurse -Path $SourceDir);
$DestFiles = #(Get-ChildItem -Recurse -Path $DestDir);
$SourceFileNames = $SourceFiles | % { $_.Name };
$DestFileNames = $DestFiles | % { $_.Name };
$MissingFromDestination = #();
$MissingFromSource = #();
$DifferentFiles = #();
foreach($f in $SourceFiles) {
if (!$DestFileNames.Contains($f.Name)) {
$MissingFromDestination += $f;
} else {
$t = $DestFiles | Where { $_.Name -eq $f.Name };
if ((Get-FileHash $f.FullName).hash -ne (Get-FileHash $t.FullName).hash) {
$DifferentFiles += $f;
}
}
}
foreach($f in $DestFiles) {
if (!$SourceFileNames.Contains($f.Name)) {
$MissingFromSource += $f;
}
}
"
Missing from Destination: "
$MissingFromDestination | % { $_.FullName };
"
Missing from Source: "
$MissingFromSource | % { $_.FullName };
"
Source is Different: "
$DifferentFiles | % { $_.FullName };
This is a bit naive in its approach insofar as it is really only checking file names and ignoring subfolder tree structures. But, hopefully, it will give you enough of a leaping off point.
I have a list of directory names that I want to convert to absolute paths, and strip out any invalid ones. My initial attempt at doing this was the pipeline
$dirs = 'dir1', 'dir2', 'dir3'
$paths = $dirs | % { Resolve-Path -ea 0 $_ } | Select -ExpandProperty Path
However, what I get back has type [Object[]] rather than [String[]]. I tried ensuring that the paths existed (by adding a ? { Test-Path $_ } step to the pipeline, but that didn't help.
What am I doing wrong? How do I get the directories as a list of strings? I need this so that I can concatenate the array to another array of strings, specifically
$newpath = (($env:PATH -split ';'), $paths) -join ';'
object[] is simply the default array in PowerShell, and it doesn't matter in this (and most) situations. You problem is that you're trying to join to arrays using (arr1, arr2). What this acutally does is create an array with two array objects, because , is an array construction operator. Try to join the arrays using +, like this:
$dirs = 'dir1', 'dir2', 'dir3'
$paths = $dirs | % { Resolve-Path -ea 0 $_ } | Select -ExpandProperty Path
$newpath = (($env:PATH -split ';') + $paths) -join ';'
and you could even skip the splitting, and just do
$dirs = 'dir1', 'dir2', 'dir3'
$paths = $dirs | % { Resolve-Path -ea 0 $_ } | Select -ExpandProperty Path
$newpath = "$env:PATH;$($paths -join ';')"
This seems to work:
$newpath = (#($env:PATH) + $paths) -join ';'
You don't really need to explicitly cast it as [string[]]. Powershell will figure that out from the command context and coerce the data to the proper type.
and thank you in advance for taking a look.
I am having an issue in a script I wrote in Powershell. The script below is a little sloppy so please forgive me.
Basically, this script takes input from a directory of text files. Each file has a line in it like so, with the following structure:
GlobalPath, AgencyPath,SitePath,SizeofSite (in bytes)
\\servername\shared, \8055\Single\department, \sitename,524835900000
The line in question is:
# Split full path and peak usage
$CalculationBuffer = $DailyBuffer[$k].Split(",")
Which results in the following error:
Method invocation failed because [System.Char] doesn't contain a method named 'Split'.
At D:\script.ps1:387 char:52
+ $CalculationBuffer = $DailyBuffer[$k].Split <<<< (",")
+ CategoryInfo : InvalidOperation: (Split:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
So my question: Is the array casted incorrectly? Since it is reporting [System.Char] instead of [System.String]?
If the file I am inputting has two lines, it does not result in this error. If the file has only one line, it gets casted as [System.Char] instead.
:Full Script:
# Monthly Output File
[string]$monthoutfile = $ProgPath + "Billing\" + $monthdate + "\_Master_" + $monthdate + ".log"
[string]$currentmonth = $ProgPath + "Billing\" + $monthdate + "\"
# Define what type of files to look for
$files = gci $currentmonth | Where {$_.extension -eq ".log"}
# Create a datastore\dictionary for this month
$MonthDataDictionary = New-Object 'System.Collections.Generic.Dictionary[string,long]'
$MonthAvgDictionary = New-Object 'System.Collections.Generic.Dictionary[string,long]'
# Arrays
$DailyBuffer = #()
$CalculationBuffer = #()
$TempArray = #()
# Counters\Integers
[int]$Linesinday = 1
[int]$DayCounter = 1
[int]$LineCounter = 0
# Strings
[string]$DailyPath = ""
[string]$Outline = ""
# Longs
[long]$DailyPeak = 0
[long]$Value = 0
##########################################################################
# Begin Loop
# Write once...
#$CalcBuffer += "\"
foreach ($file in $files)
{
# First get content from text file and store in buffer
$DailyBuffer = Get-Content $file.pspath
# Determine how many lines are in the file, call function
$Linesinday = linecount $file.pspath
for ($k = 0; $k -lt $Linesinday; $k++ )
{
# Split full path and peak usage
$CalculationBuffer = $DailyBuffer[$k].Split(",")
# Store site path
$DailyPath = $CalculationBuffer[0] + $CalculationBuffer[1] + $CalculationBuffer[2]
# Store peak usage
$DailyPeak = $CalculationBuffer[3]
# Write to dictionary under conditions
# Check if current path is stored or "Site".
# If NOT .ContainsKey($DailyPath)
if (!($MonthDataDictionary.ContainsKey($DailyPath))) {
# Add Key
$MonthDataDictionary.Add($DailyPath, $DailyPeak)
# If it does contain a value
} elseif ($MonthDataDictionary.ContainsKey($DailyPath)) {
# Add the value to the current value for averaging
$MonthDataDictionary.Item($DailyPath) += $DailyPeak
}
}
# Accumulator
$DayCounter ++
}
# Now that each file is tallied up, run an average calculation
$MonthDataDictionary.getenumerator() | Foreach-Object -process {
$Value = $_.Value / $DayCounter
$MonthAvgDictionary.Add($_.Key, $Value)
}
# Debug:
# Write-Host the values
$MonthAvgDictionary
# Output the "Average Peak" values to a file
$MonthAvgDictionary.getenumerator() | Foreach-Object -process {
# Construct output line
$OutLine = $_.Key + "," + $_.Value
$OutLine >> $MonthOutFile
}
This is a known pitfall in Powershell. Just wrap the Get-Content in an array "expression" #() :
$DailyBuffer = #(Get-Content $file.pspath)
I think the problem you're having is that get-content doesn't return an array of strings (lines), but a single string. Thus, when you look at $dailybuffer[k], you're looking at the kth character of the string, not the kth line.
I had the same problem and only the line just like below solved this problem. For your problem it will be:
[string[]] $DailyBuffer = Get-Content $file.pspath
I'm using Powershell 1.0 to remove an item from an Array. Here's my script:
param (
[string]$backupDir = $(throw "Please supply the directory to housekeep"),
[int]$maxAge = 30,
[switch]$NoRecurse,
[switch]$KeepDirectories
)
$days = $maxAge * -1
# do not delete directories with these values in the path
$exclusionList = Get-Content HousekeepBackupsExclusions.txt
if ($NoRecurse)
{
$filesToDelete = Get-ChildItem $backupDir | where-object {$_.PsIsContainer -ne $true -and $_.LastWriteTime -lt $(Get-Date).AddDays($days)}
}
else
{
$filesToDelete = Get-ChildItem $backupDir -Recurse | where-object {$_.PsIsContainer -ne $true -and $_.LastWriteTime -lt $(Get-Date).AddDays($days)}
}
foreach ($file in $filesToDelete)
{
# remove the file from the deleted list if it's an exclusion
foreach ($exclusion in $exclusionList)
{
"Testing to see if $exclusion is in " + $file.FullName
if ($file.FullName.Contains($exclusion)) {$filesToDelete.Remove($file); "FOUND ONE!"}
}
}
I realize that Get-ChildItem in powershell returns a System.Array type. I therefore get this error when trying to use the Remove method:
Method invocation failed because [System.Object[]] doesn't contain a method named 'Remove'.
What I'd like to do is convert $filesToDelete to an ArrayList and then remove items using ArrayList.Remove. Is this a good idea or should I directly manipulate $filesToDelete as a System.Array in some way?
Thanks
The best way to do this is to use Where-Object to perform the filtering and use the returned array.
You can also use #splat to pass multiple parameters to a command (new in V2). If you cannot upgrade (and you should if at all possible, then just collect the output from Get-ChildItems (only repeating that one CmdLet) and do all the filtering in common code).
The working part of your script becomes:
$moreArgs = #{}
if (-not $NoRecurse) {
$moreArgs["Recurse"] = $true
}
$filesToDelete = Get-ChildItem $BackupDir #moreArgs |
where-object {-not $_.PsIsContainer -and
$_.LastWriteTime -lt $(Get-Date).AddDays($days) -and
-not $_.FullName.Contains($exclusion)}
In PSH arrays are immutable, you cannot modify them, but it very easy to create a new one (operators like += on arrays actually create a new array and return that).
I agree with Richard, that Where-Object should be used here. However, it's harder to read.
What I would propose:
# get $filesToDelete and #exclusionList. In V2 use splatting as proposed by Richard.
$res = $filesToDelete | % {
$file = $_
$isExcluded = ($exclusionList | % { $file.FullName.Contains($_) } )
if (!$isExcluded) {
$file
}
}
#the files are in $res
Also note that generally it is not possible to iterate over a collection and change it. You would get an exception.
$a = New-Object System.Collections.ArrayList
$a.AddRange((1,2,3))
foreach($item in $a) { $a.Add($item*$item) }
An error occurred while enumerating through a collection:
At line:1 char:8
+ foreach <<<< ($item in $a) { $a.Add($item*$item) }
+ CategoryInfo : InvalidOperation: (System.Collecti...numeratorSimple:ArrayListEnumeratorSimple) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
This is ancient. But, I wrote these a while ago to add and remove from powershell lists using recursion. It leverages the ability of powershell to do multiple assignment . That is, you can do $a,$b,$c=#('a','b','c') to assign a b and c to their variables. Doing $a,$b=#('a','b','c') assigns 'a' to $a and #('b','c') to $b.
First is by item value. It'll remove the first occurrence.
function Remove-ItemFromList ($Item,[array]$List(throw"the item $item was not in the list"),[array]$chckd_list=#())
{
if ($list.length -lt 1 ) { throw "the item $item was not in the list" }
$check_item,$temp_list=$list
if ($check_item -eq $item )
{
$chckd_list+=$temp_list
return $chckd_list
}
else
{
$chckd_list+=$check_item
return (Remove-ItemFromList -item $item -chckd_list $chckd_list -list $temp_list )
}
}
This one removes by index. You can probably mess it up good by passing a value to count in the initial call.
function Remove-IndexFromList ([int]$Index,[array]$List,[array]$chckd_list=#(),[int]$count=0)
{
if (($list.length+$count-1) -lt $index )
{ throw "the index is out of range" }
$check_item,$temp_list=$list
if ($count -eq $index)
{
$chckd_list+=$temp_list
return $chckd_list
}
else
{
$chckd_list+=$check_item
return (Remove-IndexFromList -count ($count + 1) -index $index -chckd_list $chckd_list -list $temp_list )
}
}
This is a very old question, but the problem is still valid, but none of the answers fit my scenario, so I will suggest another solution.
I my case, I read in an xml configuration file and I want to remove an element from an array.
[xml]$content = get-content $file
$element = $content.PathToArray | Where-Object {$_.name -eq "ElementToRemove" }
$element.ParentNode.RemoveChild($element)
This is very simple and gets the job done.