0

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"}
3
  • 1
    "I believe you can only use 2 IF statements a single powershell script?" No, that would make it quite terrible to use. It may help if you output $Group after calling Get-Group. We don't have that file (I think), so we can't really test it for you. Commented Jun 1, 2022 at 19:03
  • your variable is scoped to that function, assign it directly to the variable from the outside: $Group = Get-Group or use scope assignments. Commented Jun 1, 2022 at 19:29
  • 1
    I can imagine someone saying something about using a switch statement instead of multiple consecutive if statements, but you certainly don't have to. Commented Jun 1, 2022 at 19:33

2 Answers 2

3

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I tried the function code you provided (Thank you!) But it still seems to be throwing the same issues. I also tried it this way, but no joy as well. Function Get-Group {$Group = ([xml](Get-Content D:\Tools\SystemInformation\SystemInformation.xml)).'system-information'.'device-group' Return $Group} Get-Group
@DamenPriddy If none of the snippets in my answer work (and give the same issues), it's likely because of something related to your xml file or something else beyond what you've provided in your question.
Nice answer! Note that return isn't really necessary in PowerShell to output from a function, due to PowerShell's implict output behaviour, which returns (outputs) anything that is not assigned to a variable or redirected or piped into another command.
0

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'"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.