Author Posts

November 9, 2017 at 2:52 pm

Hi
I am trying to create a script there reading servers diskspace.
I have run the script from a client to the domain server.
Here is a part of the script;
The problem is, I have got access denied

The function of the right is..

function Set-Rättigheter{
    #Param tillater å sende argumenter til funksjonen
    param(
        [Parameter(Mandatory=$false)]
        $ip
    )
    $sjekk = $false
    $KollaDomain = $false
    Set-ExecutionPolicy Unrestricted -Force
   
    
         #Hvis funksjonen har mottatt argument til ip variabelen skal en ikke gå inn i if-setning så lenge sjekk er usann. 
        if($ip -eq $null){
            $ip = Read-Host "Skriv in ip adressen till domänen kontrollanten"

            # Ger Trust
            $DCServer =[System.Net.Dns]::GetHostEntry($ip).Hostname
            set-item wsman:\localhost\Client\TrustedHosts -value $DCServer

           
            $username = Read-Host "Skriv in ditt Admin konto"
            $passord = Read-Host "Skriv in passord" -AsSecureString

            #Oppretter et PSCredential objekt 
            $Global:tilgang = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$passord
            $KollaDomain = $true
        }
        
        
        #Oppretter en ny powershell sesjon til en ekstern maskin
        $Global:s = New-PSSession -ComputerName $ip -Credential $tilgang -ErrorVariable err

        if($KollaDomain -eq $true){
            #Henter ut domeneinformasjon
            $domain = Invoke-Command $s {Get-ADDomain}
            #Henter ut domenenavnet fra domeneinformasjonen
            $domain = $domain.name

            #Legger domenenavnet sammen med brukernavn.
            $username = $domain + "\" + $username
            #Oppretter innloging informasjon til domenet
            $Global:tilgang = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$passord
        }

        #Utskrift hvis ikke maskin kan kontaktes
        if($err[0]-ne $null){
            Write-Host "Fikk ikke kontakt med ekstern maskin"
        }

    return $ip
} 

and the error message is ...

New-PSSession : [192.xxx.xxx.xxx] Connecting to remote server 192.xxx.xxx.xxx failed with
the following error message : The WinRM client cannot process the request. Default aut
hentication may be used with an IP address under the following conditions: the transpo
rt is HTTPS or the destination is in the TrustedHosts list, and explicit credentials a
re provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the Trust
edHosts list might not be authenticated. For more information on how to set TrustedHos
ts run the following command: winrm help config. For more information, see the about_R
emote_Troubleshooting Help topic.
At C:\Scripts\statistik.ps1:40 char:21
+ ... $Global:s = New-PSSession -ComputerName $ip -Credential $tilgang -Err ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRun
space) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotUseIPAddress,PSSessionOpenFailed
Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The argument is
null. Provide a valid value for the argument, and then try running the command again.
At C:\Scripts\statistik.ps1:44 char:38
+ $domain = Invoke-Command $s {Get-ADDomain}
+ ~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingVali
dationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.C
ommands.InvokeCommandCommand

November 9, 2017 at 4:19 pm

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-Sample-f7164554
or
watch the mva.microsoft.com Powershell videos, that's the example they created.

November 9, 2017 at 9:42 pm

When you come across these types of errors you need to get rid of all the noise.

Try a simple test of adding an ip to the trusted hosts (for non domain joined), remoting to it using enter-pssession and see what you get, then from there you can expand. Also check out the free ebook on this site for PowerShell Remoting
https://powershell.org/ebooks/

November 10, 2017 at 11:23 am

It looks like you're putting the NAME in the trusted hosts but using the IP ADDRESS in New-PSSession. That won't work. You have to be consistent.

If the 2 machines are in the same domain you don't need to worry about the trusted hosts – machines are trusted automatically

November 13, 2017 at 11:46 am

— Richard Siddaway, Can you explain more ?

November 13, 2017 at 3:10 pm

— Richard Siddaway, Can you explain more ?

November 14, 2017 at 4:06 am

In here you are adding the hostname

            # Ger Trust
            $DCServer =[System.Net.Dns]::GetHostEntry($ip).Hostname
            set-item wsman:\localhost\Client\TrustedHosts -value $DCServer

and here you are connecting to the ip address

        #Oppretter en ny powershell sesjon til en ekstern maskin
        $Global:s = New-PSSession -ComputerName $ip -Credential $tilgang -ErrorVariable err

Should it not be

            # Ger Trust
            set-item wsman:\localhost\Client\TrustedHosts -value $ip

I am hoping i have gotten it correct as i can't really read Norwegian 🙂