installing an exe but not running through computer text file

Tagged: 

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Noel Noel 2 months, 3 weeks ago.

  • Author
    Posts
  • #69574
    Profile photo of Noel
    Noel
    Participant

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

    
    $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
    
  • #69586
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Hmmm ... let's see if I got everything right. 😉
    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
            }
        }
    • #69601
      Profile photo of Noel
      Noel
      Participant

      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

  • #69592
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Why not use GPO to install the App?

  • #69619
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Wow ... strange ... 😉 ... 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
            }
        }
    • #69646
      Profile photo of Noel
      Noel
      Participant

      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";
          }
          }
      
          }
      
      
  • #69655
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    hmmm ... yes ... no ... 😉 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.

    • #69886
      Profile photo of Noel
      Noel
      Participant

      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
      
    • #69963
      Profile photo of Noel
      Noel
      Participant

      Hi Olaf,

      Ok, I did it like this. Thought I'd update you. 🙂

      $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
              }
      
      

You must be logged in to reply to this topic.