Link to home
Start Free TrialLog in
Avatar of Mike
MikeFlag for United States of America

asked on

Need help with a PowerShell Script

Greeting Experts,

I need help updating the following script below with a function to ask for the user's credentials before starting the script. The purpose of the script is to copy files from a UNC path and copy them to a list of pc's in a text file, then execute the command to install the software. Can somebody help me with this script? .


$computers = Get-Content -“Path C:\Computers.txt"
foreach ($pc in $computers) {
    {
        Copy-Item "\\domain.com\ens_software\deployment\Folder\program.exe" -Destination "\\$pc\c$\Folder" -Force -Recurse
        Invoke-Command -ComputerName $pc -ScriptBlock {"C:\Folder\program.exe /install /quiet /norestart "}
    }
    Remove-Item -Path "\\$pc\c$\Folder"

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Where?

$computers = Get-Content -“Path C:\Computers.txt"
$credential = get-credential
foreach ($pc in $computers) {
    {
        Copy-Item "\\domain.com\ens_software\deployment\Folder\program.exe" -Destination "\\$pc\c$\Folder" -Force -Recurse -credential $credential
        Invoke-Command -ComputerName $pc -ScriptBlock {"C:\Folder\program.exe /install /quiet /norestart "} -credential $credential
    }
    Remove-Item -Path "\\$pc\c$\Folder"

Open in new window

or here?
$computers = Get-Content -“Path C:\Computers.txt"

foreach ($pc in $computers) {
    {
$credential = get-credential

        Copy-Item "\\domain.com\ens_software\deployment\Folder\program.exe" -Destination "\\$pc\c$\Folder" -Force -Recurse -credential $credential
        Invoke-Command -ComputerName $pc -ScriptBlock {"C:\Folder\program.exe /install /quiet /norestart "} -credential $credential
    }
    Remove-Item -Path "\\$pc\c$\Folder"

Open in new window

In the second case you should add a prompt containing the computer name:
    $credential =Get-Credential -Message "Login for $pc"

Open in new window

BTW you should not store data in variables if you do not need to. Your first two lines can be compressed to the following without losing anything:
foreach ($pc in Get-Content -Path 'C:\Computers.txt') {

Open in new window

Your first line is wrong anyway, the double quotes must surround the path instead of the complete option:
Get-Content -Path "C:\Computers.txt"

Open in new window

Avatar of Mike

ASKER

Thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.