How to add reg file to a remote Machine with Powershell

If you need to add an exported regfile to a remote machine, try with this powershell code:

Copy the content of RegFile on lines 2-9 and change “MyComputer01” with your remote hostname.

Advertisements

How to Write on the same line with Write-Output

Powershell cmdlet Write-Output on some case is preferable instead of Write-Host; I need to use Start-Transcript and (probably a bug) Write-Host do not make any entry on transcript file.

But Write-Output is missing some nice features like -nonewline.. I need to append a string on the same line of previous output:

try {
    start-transcript -path C:\transcript.txt -Append
    # My Job
    Write-Output "1 - Job one"
    [console]::setcursorposition($([console]::Cursorleft + 25),$([console]::CursorTop - 1))
    Move-Item C:\Test.txt C:\TestDone.txt -force -ea Stop
    Write-Output "    [OK]"
}
catch [Exception] {
    Write-Output "    [FAIL]"
    $ScriptError = $_.Exception.Message
    Write-Warning $ScriptError
}
finally {
    Stop-Transcript
}

The trick was done by:

[console]::setcursorposition

[console]::Cursorleft is your current column
[console]::CursorTop is you current line

Probably with the same trick you can overwrite the previews output…

How to Disable SMB1

WannaCry and Co. suggest that it’s time to remove some old protocol, and SMB1 it’s one of that.

Here you find a script that is usefull for that; run it as admin rights:

Disable-SMB1.cmd

This batch call a powershell script (check that all the scripts is stored on the same folder and that the execution path is where you store the scripts)

I used a batch to bypass the powershell signature and make easier for helpdesk to runas as admin.

For disable it with GPO, check this:
https://support.microsoft.com/en-us/help/2696547/how-to-enable-and-disable-smbv1-smbv2-and-smbv3-in-windows-and-windows

How to Delete WSD printer Port

I had a Windows 2012 R2 printserver where a WSD port was still there after printer deletion. Delete only the port was not possible (operation not permit).

The spooler will delete the port only if it is assigned to a printer and you are deleting the printer. As workaround you can create a fake printer using this port and a preinstalled driver, then delete the printer.

But using powershell is possible to work less…

List all the WSD Printer Port:

Get-PrinterPort -Name wsd*

Delete the WSD Printer Port (PortName):

Remove-PrinterPort -Name PortName

or if you have only one WSD port:

Remove-PrinterPort -Name (Get-PrinterPort -Name wsd*).name

How to create Print queue with Powershell

Due to a new printserver rollout I write a powershell script to easily install network printers on the new Windows 2012 R2 printserver.

The script can be found here: https://github.com/lscarso/Powershell/blob/b6eb1a31790774a0433765551c9aeb455cedbd76/New-ProvisioningPrinter/New-ProvisioningPrinter.ps1

If the driver is not on driver store of your printserver, you need to install it before via GUI or using pnputil and Add-PrinterDriver:

pnputil -i -a "C:\Temp\Printers\Ricoh_UniDrv_plc6\x2DSPYP.inf"
Add-PrinterDriver -name "RICOH PCL6 UniversalDriver V4.12"

To create a new Printer Port and the Printer then just run this command:

.\New-ProvisioningPrinter.ps1 -ComputerName MYPRINTSRV -PrinterName PR01 -PrinterAddress 192.168.1.1 -PrinterLocation "Italy, Milano, Reception" -PrinterComment "Asset Number: 01987" -PrinterDriver "RICOH PCL6 UniversalDriver V4.12"

Change ComputerName to your printserver, PrinterName to the name of new print queue you want to create, the PrinterAddress to the IP/HostName of network printer, PrinterDriver to the label of driver to use.

The script create the print queue PR01, shared with the same name and a printer port linked to that printer, named with the IP or HostName you specified on PrinterAddress parameter. SNMP is enabled too on “public” community.

You can also pipe a CSV file with PrinterName, PrinterAddress, PrinterLocation, PrinterComment columns to this script for bulk printers creation:

Import-Csv "C:\Data\Working\printers.csv" -delimiter ";" -Encoding UTF8 | .\New-ProvisioningPrinter.ps1 -ComputerName PRINTSRV

You have now all the printers installed to the new printserver. You need now to configure the default settings:

Set-PrintConfiguration -ComputerName MYPRINTSRV –PrinterName PR01 -PaperSize A4 -Color $False

this configure the PR01 printer with default Paper Size to A4 and to Black & White.