Gå til innhold

PowerShell-script blir stoppet et eller annet sted i nettverket


Anbefalte innlegg

Jeg har funnet et script som kan brukes til å endre navn på maskiner i nettverket. Scriptet er i PowerShell og jeg får en feilmelding om at det stoppes på veien. Noen som vet hva som stopper dette? Script og feilmelding er under. 

#############################################################################################
# Parameterlist
#############################################################################################
param([string] $computername,[string] $newcomputername)
 
#############################################################################################
# Module import
#############################################################################################
# For checking if the computer exists in AD
import-module activedirectory
 
#############################################################################################
# Variable
#############################################################################################
$ADUserName = "konto"       # Don't forget to add your domain
$ADUserPassword = "passord"
 
#############################################################################################
# POWERSHELL Ping tool to check if a computer/ip/hostname is online
#############################################################################################
function isComputerOnline( $comp ){
    trap [Exception] {
        return $false
   }
    if ( $(new-object system.net.networkinformation.ping).send( $comp ).status -eq "Success" )
    {
        return $true
    }else{
        return $false
    }
}
 
#############################################################################################
# Remote Rename Computer by WMI
#############################################################################################
function COMPUTER_RENAME([string] $oldComputerName, [string] $newComputerName){
# should implement further checks for the computername. If computername uses illegal characters and if computername is longer than 15 characters
# More information here: http://labmice.techtarget.com/articles/computernaming.htm
    $oldComputerName = $oldComputerName.replace(" ","");
    $newComputerName = $newComputerName.replace(" ","");
    # // Check if the computer is online
    if ( isComputerOnline $oldComputerName ){
# // Check the active directory if a computer with your provided newcomputername already exists.
# // If you want to use the script without active directory just comment (use: #) this line and the corresponding "else" loop
        if ( ! (Get-ADObject -ldapfilter  "(CN=$newComputerName)") ){
            # // Handle every upcoming error as a stop failure, so you can trap it
            $ErrorActionPreference = "Stop"
            try{
                # // Get the WMI Object of the remote computer
                $ComputerWMIObject = Get-WmiObject Win32_ComputerSystem -ComputerName "$oldComputerName" -Authentication 6
                if ( $ComputerWMIObject ){
                    # // Rename the Computer Object with your or some admin credentials (Yes, Passwort is the second parameter and username the third )
                    $result = $ComputerWMIObject.Rename("$newComputerName", $ADUserPassword , $ADUserName )
                    # // Switch Case for the returnvalue of computer renaming function
                    switch($result.ReturnValue)
                    {
                        0 {
                            # // Reboot the computer instantly if renaming was successfull
                            Get-WmiObject Win32_OperatingSystem -ComputerName "$oldComputerName" |
                            ForEach-Object {$restart = $_.Win32Shutdown(6)}
                            write-host "Computer $oldComputerName was renamed ($newComputerName) and restarted"
 
                        }
                        5 { write-host "Computer was not renamed. Please check if you have admin permissions (ReturnCode 5)"; exit; }
                        default { write-host "ReturnCode $($result.ReturnValue)"; exit;}
                    }
                }else{
                    write-host "Couldn't create WMI Object on $oldComputerName"
                }
            }catch{
                write-host $_
            }
        }else{
            write-host "There is already a computerobject with the name $($newComputerName)"
        }
    }else{
        write-host "Computer is offline!"
    }
}
 
if ( $computername -and $newcomputername ){
COMPUTER_RENAME $computername $newcomputername
}else{
write-host ""
write-host "Script Usage: RenameComputer.ps1 -computername ""Current-Computer-Name"" -newcomputername ""New-Computer-Name"""
write-host ""
}

Feilmelding:

 

Ingen tilgang. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Endret av ilpostino
Lenke til kommentar
Videoannonse
Annonse

Integrated Scripting Environment. Lar deg debugge powershell script ved å kjøre linje for linje, kjøre med breakpoints osv. Avhengig av windows versjon du bruker må du kanskje søke den frem eller laste den ned. 

 

Det stopper opp på "import activedirectory" modulen til meg. Den må legges inn, og på systemene jeg har tilgang til kan jeg ikke gjøre slikt.

Endret av Flippo
Lenke til kommentar

Jeg tror jeg fant ut av det. Brannmuren på "målmaskinen" stoppet Windows Management Instrumentation (WMI-In). 

 

Det fungerte ved å bruke kommandoen under med rettelser i brannmuren.

 

 

Rename-Computer -ComputerName <gammelt_navn> -NewName <nytt_navn> -DomainCredential Domain01\Admin01 -Force -PassThru 
  • Liker 1
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...