I am attempting to create a powershell script that can perform the following:
1-Filter by Get-Group on a device via an installed xml file.
2-Only perform the copy function if the group equals Get-Group.
So far, the only thing I've succeeded in having it copy the first or the last file, on an incorrect device. I believe you can only use 2 IF statements a single powershell script? If so, how would I achieve this result I am looking for? I've also tried ElseIF, Else, Switch, and WhatIF with no joy. I have also tried different arguments for the IF statements. -eq, =, -like, and -match. None of which seem to be overly helpful in this situation.
$ActiveFilePath = "C:\ProgramData\JKCS\jkupdate\jku.ini"
Function Get-Group
{$Group = ([xml](Get-Content D:\Tools\SystemInformation\SystemInformation.xml)).'system-information'.'device-group'}
Get-Group
If ($Group -eq "XXXXX101Master")
{Copy-Item ".\JKU Files\101\jku.ini" "$ActiveFilePath"}
#----------------------------------------------#
If ($Group -eq "XXXXX102Master")
{Copy-Item ".\JKU Files\102\jku.ini" "$ActiveFilePath"}
#----------------------------------------------#
If ($Group -eq "XXXXX103Master")
{Copy-Item ".\JKU Files\103\jku.ini" "$ActiveFilePath"}
#----------------------------------------------#
If ($Group -eq "XXXXX104Master")
{Copy-Item ".\JKU Files\104\jku.ini" "$ActiveFilePath"}
#----------------------------------------------#
If ($Group -eq "XXXXX105Master")
{Copy-Item ".\JKU Files\105\jku.ini" "$ActiveFilePath"}
#----------------------------------------------#
If ($Group -eq "XXXXX106Master")
{Copy-Item ".\JKU Files\106\jku.ini" "$ActiveFilePath"}
CodePudding user response:
The assignment to $Group
during the execution of your Get-Group
function is not visible to the rest of the script. You could have the function return the value instead, and then assign it to $Group
outside the function, like this:
Function Get-Group
{return ([xml](Get-Content D:\Tools\SystemInformation\SystemInformation.xml)).'system-information'.'device-group'}
$Group = Get-Group
Or, use a scope modifier in the assignment within the function to make it visible to the whole script:
Function Get-Group
{ $Script:Group = ([xml](Get-Content D:\Tools\SystemInformation\SystemInformation.xml)).'system-information'.'device-group'}
Get-Group
Or, if you don't need the function anywhere else, just directly assign $Group
like this (at Script scope):
$Group = ([xml](Get-Content D:\Tools\SystemInformation\SystemInformation.xml)).'system-information'.'device-group'
See the help document "about_Scopes" (i.e. run help about_Scopes
).
CodePudding user response:
I would change your Get-Group function somewhat to use
function Get-Group {
# if you load the xml file this way, you are ensured to get the file encoding correct
$xml = [System.Xml.XmlDocument]::new()
$xml.Load('D:\Tools\SystemInformation\SystemInformation.xml')
$xml.'system-information'.'device-group'
}
Then, if all your groups have the same format of "XXXXX" numeric value "Master" you could do this:
$group = Get-Group
$subfolder = [regex]::Match($group, '(\d )Master$').Groups[1].Value
if (![string]::IsNullOrWhiteSpace($subfolder)) {
$path = Join-Path '.\JKU Files' -ChildPath ('{0}\jku.ini' -f $subfolder)
Copy-Item -Path $path -Destination $ActiveFilePath
}
else {
Write-Warning "Could not find a matching subfolder name in '$group'"
}
Otherwise, if the group names do not all have the same format, I would suggest creating a lookup Hashtable where you can match the group name with the path of the ini file
$lookup = @{
'XXXXX101Master' = 101
'XYZ102Demo' = 102
'103Copy' = 103
'X123456789104Huh' = 104
# etc.
}
$group = Get-Group
if ($lookup.ContainsKey($group)) {
$subfolder = $lookup[$group]
$path = Join-Path '.\JKU Files' -ChildPath ('{0}\jku.ini' -f $subfolder)
Copy-Item -Path $path -Destination $ActiveFilePath
}
else {
Write-Warning "Could not find a matching subfolder name in '$group'"
}