Avatar of jskfan
jskfan
Flag for Cyprus asked on

pass username and password in batch file

pass username and password in batch file

I have this Script that adds a printer on each computer listed on text file , then at the end it calls a powershell, printer.ps1 that enables on the printer the option "Print Directly to printer"

Everything works fine.
I would like to run the script from my computer which is joined to the Domain against the computers listed on the text file which are not joined to the domain. So I need to pass username and password on this script ..the remote computers all have the same local Administrator account and password of Pass2020.

@echo off
setlocal
for /f %%a in (CompPrinterConf.txt) do (
@echo starting pnpport on %%a

 cscript.exe "C:\windows\system32\Printing_Admin_Scripts\en-US\prnport.vbs" -a  -s %%a -r "AAAA" -h hostname -o raw -n 9100
@echo Waiting 5 seconds
timeout 5
@echo starting setting MyPrinter on %%a
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a  -p "XXXX-YYYY" -m "HP Universal Printing PCL 6" -r "AAAA"  -s %%a
REM Not really needed, because the script will continue with the next machine
REM timeout 5


)
@echo starting powershell
PowerShell.exe -ExecutionPolicy Bypass -Command  c:\Scripts\printer.ps1
REM timeout 5

Open in new window

Windows BatchVB ScriptScripting Languages

Avatar of undefined
Last Comment
jskfan

8/22/2022 - Mon
Kesavan Jeganarayanan

You can try with Runas. but i haven't verified this. this is one option.

runas /profile /user:fabrikam\kenmyer “cscript.exe \” "C:\windows\system32\Printing_Admin_Scripts\en-US\prnport.vbs" -a  -s %%a -r "AAAA" -h hostname -o raw -n 9100
jskfan

ASKER
as you can see at the end it calls a powershell script printer.ps1, that has the code below.
Now do I also need to include username and password on printer.ps1 , or the calling script will pass that automatically ? which I doubt it.

Get-Content -Path C:\Scripts\CompPrinterConf.txt | ForEach-Object {


$printer = Get-WmiObject -Class Win32_Printer -Filter "Name = 'Myprinter'" -ComputerName $_
$printer.Direct= $true
$printer.Put()
}

Open in new window

ASKER CERTIFIED SOLUTION
NVIT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jskfan

ASKER
The powershell ps1 will run with the same credentials. If you admin are domain admin, it should work.
it would have been simple that way...

but the script is on  a computer joined to the domain, and the target computers are in Work Group
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
jskfan

ASKER
I will try it later
Thank you