Rechercher dans ce blog

mardi 6 février 2018

Powershell - Renommer une machine si elle existe dans landesk (webservice)

$Log = 'C:\Windows\Logs\RenamecomputerLog.log'
$URILandesk = "http://----YOURSERVER----/MBSDKService/MsgSDK.asmx"

function Write-Log {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias("LogContent")]
        [string]$Message,

        [Parameter(Mandatory=$false)]
        [Alias('LogPath')]
        [string]$Path= $Log,
        
        [Parameter(Mandatory=$false)]
        [ValidateSet("Error","Warn","Info")]
        [string]$Level="Info",
        
        [Parameter(Mandatory=$false)]
        [switch]$NoClobber
    )

    Begin
    {
        # Set VerbosePreference to Continue so that verbose messages are displayed.
        $VerbosePreference = 'Continue'
    }
    Process
    {
        
        # If the file already exists and NoClobber was specified, do not write to the log.
        if ((Test-Path $Path) -AND $NoClobber) {
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
            Return
            }

        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
        elseif (!(Test-Path $Path)) {
            Write-Verbose "Creating $Path."
            $NewLogFile = New-Item $Path -Force -ItemType File
            }

        else {
            # Nothing to see here yet.
            }

        # Format Date for our Log File
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        # Write message to error, warning, or verbose pipeline and specify $LevelText
        switch ($Level) {
            'Error' {
                Write-Error $Message
                $LevelText = 'ERROR:'
                }
            'Warn' {
                Write-Warning $Message
                $LevelText = 'WARNING:'
                }
            'Info' {
                Write-Verbose $Message
                $LevelText = 'INFO:'
                }
            }
        
        # Write log entry to $Path
        "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
    }
    End
    {
    }
}

############################################################
#
#    CONNECT LANDESK
$Server = New-WebServiceProxy -Uri ($URILandesk) -UseDefaultCredential

############################################################
#
#    GET ALL DEVICES
$AllMachines = $Server.ListMachines("").Devices

############################################################
#
#    GET BIOS SERIAL NUMBER LOCAL
$SerialNumberBiosLocal = (Get-WmiObject -Class Win32_Bios | select SerialNumber).SerialNumber

############################################################
#
#    FIND CORRESPONDANCE IN LANDESK
$computersystem = Get-WmiObject -Class win32_computersystem

Foreach($Machine in $AllMachines)
{
   $LandeskSerialNumber = $Server.GetMachineData($Machine.guid, 'Computer.Bios."Serial Number"').MachineData.Value
     
   if($SerialNumberBiosLocal -eq $LandeskSerialNumber)
   {
        $match = $Machine.DeviceName
        Write-Log -Message "----------------------------------" -Level Info
        Write-Log -Message $Machine.DeviceName -Level Info
        Write-Log -Message $Machine.guid -Level Info
        Write-Log -Message $LandeskSerialNumber -Level Info
        Write-Log -Message "----------------------------------" -Level Info
        break;
   }
}
function FormRename {
    Add-Type -AssemblyName System.Windows.Forms

    $FormRenameComputer = New-Object system.Windows.Forms.Form
    $FormRenameComputer.Text = "Rename Computer"
    $FormRenameComputer.TopMost = $true
    $FormRenameComputer.Width = 431
    $FormRenameComputer.Height = 184

    $textBox2 = New-Object system.windows.Forms.TextBox
    $textBox2.Width = 312
    $textBox2.Height = 20
    $textBox2.location = new-object system.drawing.point(45,58)
    $textBox2.Font = "Microsoft Sans Serif,10"
    $FormRenameComputer.controls.Add($textBox2)

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Width = 150
    $Label1.Height = 20
    $Label1.Text = "Rename Computer"
    $Label1.location = new-object system.drawing.point(45,20)
    $Label1.Font = "Microsoft Sans Serif,10"
    $FormRenameComputer.controls.Add($Label1)

    $button2 = New-Object system.windows.Forms.Button
    $button2.Text = "OK"
    $button2.Width = 60
    $button2.Height = 30
    $button2.location = new-object system.drawing.point(340,100)
    $button2.Font = "Microsoft Sans Serif,10"
    $FormRenameComputer.controls.Add($button2)

    $button2.Add_MouseClick({
        #$computersystem.Rename($textBox2.Text.Trim())
        $FormRenameComputer.Close()
      })


    [void]$FormRenameComputer.ShowDialog()
    $FormRenameComputer.Dispose()
}


If($computersystem.Name -eq $Machine.DeviceName)
{
    ############################################################
    #
    #    PROTECT
    Write-Log -Message "Rename computer : cannot be rename because the computer name is the same" -Level Info
    FormRename
}
else
{
    ############################################################
    #
    #    RENAME COMPUTER
    "Rename computer with : $($Machine.DeviceName)"
    $computersystem.Rename($Machine.DeviceName)
}