Help w/ Invoke-Command and If statement

Hey guys,

Working on a project designed to find expired licenses for a 3rd party app called TSPrint. I’ve got a working command that returns a list of PSComputerNames with expired licenses based the behavior of a specific exe and thats working great. What I’m struggling with though is once I’ve identified these servers with expired licenses I’d also like to check them to see if a file exists in another directory @ ‘C:\Program Files (x86)\TerminalWorks\TSScan Server\TSSCan.twlic’. The end result I’m hoping for is a list of expired servers + confirmation if that 2nd file exists on the expired servers or not (separated into 2 columns). Heres what I have so far if anyone can help point a noob in the right direction Id appreciate a 2nd look.

# Query all Domains
    $Domains = GetADTrustDomains -AllDatacenters
    $Members = foreach ($domain in $domains) {
               # Collect all Servers               
               Get-ADComputer -Server $domain -Filter * | Select-Object -ExpandProperty DNSHostName
    }
    
    Write-Host 'Locating Severs w/ expired TSPrint.twlic license files...'

    # Query Servers for previous instance of PAID TSPrint licensing
        # Start PDFWriter.exe, wait 10s and check persistence

    Invoke-Command -ComputerName $Members -ScriptBlock { 
    $PreviouslyLicensed = Test-Path 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\TSPrint.twlic'                                                                                                                                            
    if($PreviouslyLicensed -ne $null)
    {
    Start-Process 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\pdfwriter.exe'
    Start-Sleep -Seconds 10
    Get-Process -Name PDFwriter
    }

 

 

 

$result = 
 Invoke-Command -ComputerName $Members -ScriptBlock { 
    $path = 'C:\Program Files (x86)\TerminalWorks\TSScan Server\TSSCan.twlic'        
    [PSCustomObject]@{FileCheck="{0}: $(split-path $path -leaf)" -f (Test-Path $path)}        
 }

# Display servername and filecheck
$result | Select-Object * -ExcludeProperty RunspaceId

Im not following. $Members is only being used as a variable for server names when trying to query for servers with an expired license. I dont want to check for the TSScan.twlic file on all servers ONLY one ones that have been identified as expired. In order for it to identify an expired server it has to run the command referenced below. Its not until after that has ran that I have the list of expired servers that needs to be checked for the additional file.

Background: This command is verifying that a paid license for TSPrint exists by checking for the Tsprint.twlic file. Once that has returned true it starts the pdfwriter.exe and waits 10 seconds to see if the process is still running. The exe behavior is that if the license is invalid the process will remain open and if its valid the process will close after 1s. Once ive found the servers where this process is remaining open I can assume they are unlicensed and then would like to check for the TSScan.twlic license to see if the sister app TSScan also has a paid license. That app has no behavior for identifying an expired license but if TSprint is expired you can assume TSSCan is also since they only break due to mac address changes so its important that its included if its present on the system or not.

Invoke-Command -ComputerName $Members -ScriptBlock { 
    $PreviouslyLicensed = Test-Path 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\TSPrint.twlic'                                                                                                                                            
    if($PreviouslyLicensed -ne $null)
    {
    Start-Process 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\pdfwriter.exe'
    Start-Sleep -Seconds 10
    Get-Process -Name PDFwriter
    }

Didn’t read the whole post, but from a quick look, Test-Path always returns boolean value which is never null, so the if statement always executes.

You just need to below.

if($PreviouslyLicensed){
   # statement
}

That makes sense thank you for the tip. I still cant wrap my mind around checking for the 2nd file though. As I see it the PSComputerName isnt returned until the invoke-command has completely ran so there is no way to continue the search within the initial script block. With my limited knowledge I have a few ideas but Im not sure if they are good or not…

  1. Assign PScomputernames to a variable and invoke a 2nd command (does using invoke twice seem messy?)
  2. Use Get-Childitem (Returns PSComputerName remotely whereas test-path does not) to test both file paths and then compare and return only matching pscomputernames
[quote quote=206397]Didn’t read the whole post, but from a quick look, Test-Path always returns boolean value which is never null, so the if statement always executes.

You just need to below.

PowerShell
4 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
if($PreviouslyLicensed){
# statement
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

If I am understanding correctly, this should work. Also, it would be faster to do both checks in the same invoke-command session.

$result = 
Invoke-Command -ComputerName $Members -ScriptBlock { 
    # Check for TSPrint license
    $path1 = 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\TSPrint.twlic'                                                                                                                                            
    If(Test-Path $path1){
        Start-Process 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\pdfwriter.exe'
        Start-Sleep -Seconds 10
        $process = Get-Process -Name PDFwriter
    }

    # If PDFWriter is still open then check for second file
    If ($process.HasExited -eq $false){
        $path2 = 'C:\Program Files (x86)\TerminalWorks\TSScan Server\TSSCan.twlic'        
        [PSCustomObject]@{
            ServerName = $env:COMPUTERNAME
            FileCheck = (Test-Path $path2)        
         } 
    }
}

# Output all servers without second file (no license)
$result | Where-Object {$_.FileCheck -eq $false}

[quote quote=206583][/quote]

This is a great example. I was already playing around with assigning a variable to get process but hadnt thought of using if + hasexited property on that variable to run a secondary check within the same script block. Brilliant lol. Definitely returning the results I was looking for and not as complicated as I thought itd be. Thank you for the 2nd look I really appreciate it!

As I understand it, you could cut down on a couple of lines by using -Passthru

$process = Start-Process 'C:\Program Files (x86)\TerminalWorks\TSPrint Server\pdfwriter.exe' -PassThru | Get-Process