Basic ForEach and IF statements

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 1 year, 10 months ago.

  • Author
    Posts
  • #22195
    Profile photo of Adam Irinaga
    Adam Irinaga
    Participant

    Hey everyone,

    Not the best at writing these things... I feel like I have the right syntax down. If someone could help out that'd be great! Everything executes fine but doesn't end up creating my scheduled task.

    Param(
    [Parameter(Mandatory=$True, Position=1)]
    [string[]] $computernames,
    [Parameter(Mandatory=$True, Position=2)]
    [system.management.automation.PSCredential] $credential,
    [Parameter(Mandatory=$True, Position=3)]
    [String] $date,
    [Parameter(Mandatory=$True, Position=4)]
    [String] $time
    )

    foreach($server in $computernames){

    Invoke-Command -cn $server -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | select DisplayName}

    foreach($Name in $server){

    If($Name.DisplayName -like "Microsoft Office Office 64-bit Components 2007"){

    Invoke-Command -Computer $server -Credential $credential -ScriptBlock {
    param($date, $time)
    schtasks /create /tn "Uninstall Office 2007" /tr "\\Fileserver03\Install\Microsoft\Office\2007\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2007\SilentUninstall.xml" /sc Once /sd $date /st $time /f /V1 /z /ru System
    } -ArgumentList $date, $time}

    If($Name.DisplayName -like "Microsoft Office Professional Plus 2010"){

    Invoke-Command -Computer $server -Credential $credential -ScriptBlock {
    param($date, $time)
    schtasks /create /tn "Uninstall Office 2010" /tr "\\Fileserver03\Install\Microsoft\Office\2010\Uninstall\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2010\Uninstall\SilentUninstall.xml" /sc Once /sd $date /st $time /f /V1 /z /ru System
    } -ArgumentList $date, $time}

    If($Name.DisplayName -like "Microsoft Office 64-bit Components 2013"){

    Invoke-Command -Computer $server -Credential $credential -ScriptBlock {
    param($date, $time)
    schtasks /create /tn "Uninstall Office 2013" /tr "\\Fileserver03\Install\Microsoft\Office\2013\Admin\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2013\Admin\SilentUninstall.xml" /sc Once /sd $date /st $time /f /V1 /z /ru System
    } -ArgumentList $date, $time}

    }
    }

  • #22196
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Your second foreach statement doesn't make any sense. You input an array of computernames and the first foreach iterates through those. Inside that loop you have another that starts with:

    foreach ($Name in $Server)

    What is it that you're trying to loop through? At that point server is only a single string so there is no more looping you can do with it.

    Beyond that, in your OP you haven't even stated that your script does not work. It will help if you can give us an idea what you're seeing. Do you get any errors?

  • #22206
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Adam,

    To add to Matt's comment, one thing i see is that you are not assigning a value to the resolve of the initial Invoke-Command

       Invoke-Command -cn $server -ScriptBlock {
            Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -Property DisplayName
        }
    

    This won't do anything other than output this to your screen.

    Also, I see you are using the -like operator. This also doesn't make sense, since you are not including any wildcards. The result will be the same, but if it's the exact name, then it's nicer to use '-eq'

    I think what you're wanting to loop through will be the results from the previous Invoke. If that being the case, your code would be something like below. NB I've also used -expandproperty since you only require one property of the object that is being passed through the pipeline.

    Param(
        [Parameter(Mandatory = $True, Position = 1)]
        [string[]] $computernames,
        [Parameter(Mandatory = $True, Position = 2)]
        [system.management.automation.PSCredential] $credential,
        [Parameter(Mandatory = $True, Position = 3)]
        [String] $date,
        [Parameter(Mandatory = $True, Position = 4)]
        [String] $time
    )
    
    foreach($server in $computernames)
    {
        $applications = Invoke-Command -cn $server -ScriptBlock {
            Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty DisplayName
        }
    
        foreach($application in $applications)
        {
            If($application -like 'Microsoft Office Office 64-bit Components 2007')
            {
                Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
                    param($date, $time)
                    schtasks.exe /create /tn 'Uninstall Office 2007' /tr '\\Fileserver03\Install\Microsoft\Office\2007\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2007\SilentUninstall.xml' /sc Once /sd $date /st $time /f /V1 /z /ru System
                } -ArgumentList $date, $time
            }
    
            If($application  -like 'Microsoft Office Professional Plus 2010')
            {
                Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
                    param($date, $time)
                    schtasks.exe /create /tn 'Uninstall Office 2010' /tr '\\Fileserver03\Install\Microsoft\Office\2010\Uninstall\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2010\Uninstall\SilentUninstall.xml' /sc Once /sd $date /st $time /f /V1 /z /ru System
                } -ArgumentList $date, $time
            }
    
            If($application -like 'Microsoft Office 64-bit Components 2013')
            {
                Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
                    param($date, $time)
                    schtasks.exe /create /tn 'Uninstall Office 2013' /tr '\\Fileserver03\Install\Microsoft\Office\2013\Admin\setup.exe /uninstall ProPlus /config \\Fileserver03\Install\Microsoft\Office\2013\Admin\SilentUninstall.xml' /sc Once /sd $date /st $time /f /V1 /z /ru System
                } -ArgumentList $date, $time
            }
        }
    }
    

You must be logged in to reply to this topic.