Saturday, February 22, 2020

Spawn powershell app from cmd

Is it possible to create a powershell script from cmd or bat file?

Compress powershell script
expand and save to ps1 files
run files

Friday, January 24, 2020

Update DICOM tags with PowerShell



Using the dicom PowerShell Module



Get the DICOM Powershell Module
https://www.powershellgallery.com/packages/Dicom/1.0.10.0


Powershell Script
================================================
# Update DICOM file TAGS

# Use this command to install Dicom Module - Install-Module -Name Dicom


$curpath = $(Get-Location).Path
$targetFolder = $curpath + "\*.dcm"
$dcmfiles = Get-ChildItem $targetFolder

# Series Description Tag Names Hash table
        $seriesDescriptionS = @{
            LAP='Leg Full Left AP (Stitched)'
            LL='Leg Full Left Lat (Stitched)'
            RAP='Leg Full Right AP (Stitched)'
            RL='Leg Full Right Lat (Stitched)'
        }



#Process targeted dicom files

ForEach ($dcmfile in $dcmfiles) {

$dicomfile = Import-dicom -Filename $dcmfile
$dicom = Read-Dicom -DicomFile $dicomfile
$dicom.SeriesDescription
$PatientID=$dicom.PatientID
$newseriesDescription = $seriesDescriptionS.($dcmfile.BaseName.ToUpper())
$tempfolder = "c:\support\dcmtemp"
# Look for file names with ll,rl,rap, or rl to processs and insert tags
    if ($newseriesDescription) {


    edit-dicom -DicomFile $dicomfile -Tag "0018,1164" -Value '0.150000\0.150000'
    edit-dicom -DicomFile $dicomfile -Tag "0008,103e" -Value $newseriesDescription
    Export-Dicom -DicomFile $dicomfile -DestinationPath $tempfolder
    $tempfile = $tempfolder + "\DICOM\0000001"
    $targetFile = $curpath + "\" + $PatientID + "-" +$dcmfile.BaseName.ToUpper() + "s.dcm"
    $targetFile
    Copy-Item -Path $tempfile -Destination $targetFile


    }
}

Remove-Item $tempfolder -Force  -Recurse -ErrorAction SilentlyContinue

Thursday, January 23, 2020

Check if LLMNR is disabled with PowerShell

Check if LLMNR Is disabled with PowerShell

Get-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows NT\DNSClient" -name EnableMulticast


If it returns an error then it is not set.



example

PS C:\support\scripts> Get-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows NT\DNSClient" -name EnableMulticast


EnableMulticast : 0
PSPath          : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows
                  NT\DNSClient
PSParentPath    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows NT
PSChildName     : DNSClient
PSDrive         : HKLM
PSProvider      : Microsoft.PowerShell.Core\Registry


Get Just the value

$(Get-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows NT\DNSClient" -name EnableMulticast).EnableMulticast

Returns 0 if disabled.



Monday, December 30, 2019

To many msi*.tmp files

Found this issue on a old windows 7 machine.

upgraded to 10, but the files where still there


https://www.tenforums.com/general-support/51923-cannot-delete-tmp-folders.html


lots of msi*.tmp files

FOR /F "delims=*" %I IN ('DIR /B "C:\msi*"') DO ( TAKEOWN /F "%I" /R && RD /Q /S "%I" )

Wednesday, October 23, 2019

Check if Windows update is disabled

$(Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer -name NoWindowsUpdate).NoWindowsUpdate


if value = 1 then yes
0 = no

ref
https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-entries?view=powershell-6


Auto Update Settings
$AUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
$AUSettings



Thursday, October 17, 2019

Email files via PowerShell

# Email files
# Requires Powershell Version 5.1 or higher check with $PSVersionTable

$FolderTarget = '\\SomeFolder'
$FolderMove = "\\SomeFolder\Move"
$emailFrom = "from@domain.com"
$emailTo = "tosomeone@domain.com"
$emailSubjectPrefex = "Prefex note: "
$emailBodyPrefex = "Prefex Body: "
$smtpServerName = "smtp.domain.com"
$smtpServerPort = "25"

# Get files in Target Folder
$files = (get-childitem $FolderTarget)

# Loop through the list of files and send email
$files|ForEach-Object {
    # Select only PDF files
    if ($_.Extension -eq '.pdf') {
        $MessageHash = @{
                from        = $emailFrom
                to          = $emailTo
                subject     = $emailSubjectPrefex + $_.BaseName
                smtpserver  = $smtpServerName
                port        = $smtpServerPort
                attachments = $_.fullname
                body        = $emailBodyPrefex + $_.BaseName
            }
            Send-MailMessage $MessageHash
            # Move file to folder and add timestamp
            $ToFolder = $FolderMove + "\" + $_.BaseName + "-" `
                + $($(Get-Date -Format yymmddhhmmss-fff)) + ".pdf"
            Move-Item $_.FullName $ToFolder
    }
}

Friday, September 27, 2019

Upate Active Directory User's Manager with Powershell

Problem,

Need to replace manager for multiple users in Active Directory (AD)

Solution
First we need to find what users have a giving manager.

This script will query AD for users with given manager SAM account name.

#
#   Get AD User's Manager
#
#

Param(
    [Parameter(Mandatory=$false,
        ValueFromPipeline=$true)]
        $FindManagerSamAccountName

    ,[Parameter(Mandatory=$false,
        ValueFromPipeline=$true)]
        $FindUserSamAccountName 
)

#Check if AD is installed
$ADUsersManagers = ""
    If (!(Get-Module ActiveDirectory)) {
        Import-Module ActiveDirectory
    }

$ADUserList =""
if ([string]::IsNullOrEmpty($FindUserSamAccountName)) {
    $ADUserList = Get-ADUser -Filter { Enabled -eq $true } -Properties Manager 
    } else {
    $ADUserList = Get-ADUser -Filter {SamAccountName -eq $FindUserSamAccountName } -Properties Manager                 
    }

$ADUsersManagers = $ADUserList|Select-Object Name,SamAccountName,@{n='ManagerSamAccountName';e={(Get-aduser $_.Manager).SamAccountName}} 

   if (![string]::IsNullOrEmpty($FindManagerSamAccountName)) {
        $ADUsersManagers |Where-Object {($_.ManagerSamAccountName -eq $FindManagerSamAccountName)}
        } else {
            $ADUsersManagers}