I am trying to make a powershell script that will add a network printer to a remote PC. Pretty much if a user calls us asking for a printer, we run this script and it will be added. The only way I have been able to get it to run is via scheduled task useing a VBS script I found. I have tried many many other ways and none seem to work.
The only problem I seem to be having is with the invoke-wmimethod line. If anyone can help me with that I can get this going. Any further idea's would be helpful. Please note, I picked up my first Powershell book less than 2 weeks ago and this is my first script.
#Set PC Name
if
($args.count -ne 1) {
$pc
= Read-Host "Enter PC name"
}
Else
{$pc = $args[0]}
# Get batch file ready
$rand
= New-Object random
Set-Variable
-Name fnbat -value ([string]$rand.next(0,9) + $rand.next(0,9) + $rand.next(0,9) + $rand.next(0,9))
$fnbat
= "print" + $fnbat + ".bat"
$tmp
= Read-Host "Printer Name"
$prnt1
= "Start \\printserver1\" + $tmp
$prnt2
= "Start \\printserver2\" + $tmp
out-file
-filepath \\fileserver\share\temp\printer\$fnbat -inputobject "$prnt1 `n$prnt2" -encoding ASCII
#get times
$time
= Get-WmiObject -Class Win32_LocalTime -ComputerName $pc
$rmin
= $time.Minute+2
$h
= $time.Hour
if
($rmin -ge 60) {
$rmin
= 02
$h
= $h+1
}
$fdate
= Get-Date -Year $time.Year -Month $time.Month -Day $time.Day -Hour $h -Minute $rmin -Second $time.Second -UFormat %Y%m%d%H%M%S
#Schedule the job
Invoke-WmiMethod
-Class Win32_schedulejob -Name Create -Namespace "\ROOT\CIMV2:" -ArgumentList "\\fileserver\share\Temp\printer\$fnbat, $fdate, False, 0, 0, True, 1"
The next script is the VBS script that works, but is too hard coded for what I intend.
If Wscript.Arguments.Count = 0 Then
strComputer = "."
else
strComputer = Wscript.Arguments(0)
end if
strCommand = "
\\server\share\temp\printer\print1081.bat"
Const INTERVAL = "n"
Const MINUTES = 1
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
Wscript.Echo "WSUS was started with a process ID: " & intJobID
Else
Wscript.Echo "WSUS could not be started due to error: " & errReturn
End If
Any help will be appreciated.