installing an exe but not running through computer text file

I want to install an exe and this works for one pc but when I use get-content, it only picks up one pc.
My script isn’t probably indented correctly. Could anyone help me please? I’m still learning. :slight_smile:

$computername = get-content "C:\temp\computers.txt"


foreach ($computer in $computername)

{$result = Get-WmiObject  Win32_UserProfile  -ComputerName $computer | Where-Object {($_.LocalPath -like  'C:\Users\*') -and ($_.Loaded -eq 'True')} | select LocalPath, Loaded, @{LABEL="last used";EXPRESSION={$_.ConvertToDateTime($_.lastusetime)}}| Sort-Object -Property "Last Used" -Descending | Select-Object Localpath -First 1 

$localpath= $result.LocalPath
$profile=$localpath.replace("C:\Users\","")

$profile

$result = Get-WMIObject -class Win32_ComputerSystem -ComputerName $Computer | select username 
$username= $result.username
$username 
}

& 'C:\temp\newphonenumber.exe' $computer $profile $username

Hmmm … let’s see if I got everything right. :wink:
You do a lot of effort to create a variable $Profile which actually contains the username, right? A profile path without "c:\users" usually is the username.
Then you create another variable “$Username” which contains (if you’re lucky) one username (if there are more than one users on the computer you will get an array of usernames).
And in the end you run your executable locally with the parameters $Computer, $profile and $username
Probably something like this could work for you:

get-content “C:\temp\computers.txt” |
ForEach-Object {
$Computer = $_
$Users = Get-WMIObject -class Win32_ComputerSystem -ComputerName $_ | select username
Foreach($username in $Users){
& ‘C:\temp\newphonenumber.exe’ $computer $username $username
}
}

Why not use GPO to install the App?

Hi Olaf, That’s the catch. The $profile isn’t the same as the $username. We changed the usernames in AD a while ago without renaming the C:\users\username folder. So we have the old username in here and what I’ve done is look at the newest profile in the C:\users folder and take that username, it’s worked so far. There’s probably a nicer way to do that?
The .exe file updates an .ini file in the C:\users\profilename folder.

I just have to combine $username and $profile in the script. Do you know how I’d add the $profile part into your suggested script above?

Thanks
Noel

Wow … strange … :wink: … try it this way:

get-content “C:\temp\computers.txt” |
ForEach-Object {
$Computer = $_
$profile = Split-Path -Leaf -Path (Get-WmiObject Win32_UserProfile -ComputerName $computer |
Where-Object {($.LocalPath -like ‘C:\Users*’) -and ($.Loaded)} |
Select-Object -Property LocalPath, Loaded, @{LABEL=“last used”;EXPRESSION={$.ConvertToDateTime($.lastusetime)}} |
Sort-Object -Property “Last Used” -Descending |
Select-Object -First 1 ).LocalPath

    $Users = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $_ | select username 
    Foreach($username in $Users){
        & 'C:\temp\newphonenumber.exe' $computer $profile $username
    }
}</pre>

Hey Olaf, thanks for that. That worked great. Sorry to ask again but I probably should do some error logging. I’ve looked into try and catch and tried the following, am I on the right path?

get-content "C:\temp\computers.txt" |
    ForEach-Object {
        $Computer = $_
        $profile = Split-Path -Leaf -Path (Get-WmiObject  Win32_UserProfile  -ComputerName $computer | 
            Where-Object {($_.LocalPath -like  'C:\Users\*') -and ($_.Loaded)} | 
                Select-Object -Property LocalPath, Loaded, @{LABEL="last used";EXPRESSION={$_.ConvertToDateTime($_.lastusetime)}} | 
                    Sort-Object -Property "Last Used" -Descending | 
                        Select-Object -First 1 ).LocalPath

        $Users = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $_ | select username 
        Foreach($username in $Users)
        {
          try
           {
            & 'C:\temp\newphonenumber.exe' $computer $profile $username
            write-host "everything ran ok" " $computer,OK"; 
            add-content "C:\Temp\ok.csv"
        }
        catch
        { 
        write-host "not successful" 
        Add-content "C:\Temp\errors.csv" " $computer,Not_OK";
    }
    }

    }

hmmm … yes … no … :wink: You should start with:

Get-Help about_Try_Catch_Finally -ShowWindow
And you could search for examples in the internet or read a book or see a tutorial. I think it will be a little too much to explain it here good enough.
But in your script - You have the “everything ran ok” in the try block. So it’s executed anyway - even if your executable fails. And that’s the next thing. Does your executable throws an error Powershell will be able to recognise/catch? … or is it maybe just an output to the “standard output” instead of the “standard error output”? So I’m not sure if this really works as you expect it to work. You could think about using “Start-Process
At least IMHO the “positive” logging should not be in the try block.

hi Olaf,

Thanks for your help again. I think the last line of the script should have read “& ‘C:\temp\newphonenumber.exe’ $computer $profile $users” ? If I want to output what I have before I run the .exe, I could do something like this?

#$results = @{            
              
               $Computer                 
               $profile
	       $Users
        }                           
 $results | export-csv -Path c:\temp\so.csv -NoTypeInformation

Hi Olaf,

Ok, I did it like this. Thought I’d update you. :slight_smile:

$computername = get-content "C:\temp\computers12.txt"
 
 foreach ($computer in $computername)
    {
        $results = '' | SELECT ComputerName,Profile,Username
        $results.computername = $computer

        $results.Profile = Split-Path -Leaf -Path (Get-WmiObject  Win32_UserProfile  -ComputerName $computer | 
            Where-Object {($_.LocalPath -like  'C:\Users\*') -and ($_.Loaded)} | 
                Select-Object -Property LocalPath, Loaded, @{LABEL="last used";EXPRESSION={$_.ConvertToDateTime($_.lastusetime)}} | 
                    Sort-Object -Property "Last Used" -Descending | 
                        Select-Object -First 1 ).LocalPath

  
        $results.Username = (Get-WmiObject Win32_ComputerSystem -ComputerName $computer).Username
        
        $results |export-csv -Path c:\temp\results2.csv -Append -NoTypeInformation
        }