locked
Run Powershell to get server information from another domain server RRS feed

  • Question

  • hi everyone

    I want to Run powershell to get some server information that the servers are in another domain B and I have Domain B account

    what I facing problem is I don't know how the setting use domain B account to run my code (because original domain A without permission to access domain B)

    Actually the powershall will run in the Domain A desktop and to get information from domain B server

    my code is below

    -----------------------------------------------------------------------------------------------

    # this part is I facing problem

    $User = "Username"
    $PassWord = ConvertTo-SecureString -String "Password" -AsPlainText -Force
    $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PassWord

    # part of below this line is work if the server in the same domain 

     $Server = Read-host "Please enter server name"

    #server's CPU Mem Hardinfor
     $cpu = Get-WMIObject –computername $Server win32_Processor
     $mem = gwmi -ComputerName $Server win32_OperatingSystem
     $Disks = gwmi –Computer: $Server win32_logicaldisk -filter "drivetype=3"
     $Havecpu = "{0:0.0} %" -f $cpu.LoadPercentage
     $Allmem = "{0:0.0} MB" -f ($mem.TotalVisibleMemorySize / 1KB)
     $Freemem = "{0:0.0} MB" -f ($mem.FreePhysicalMemory / 1KB)
     $Permem = "{0:0.0} %" -f ((($mem.TotalVisibleMemorySize-$mem.FreePhysicalMemory)/$mem.TotalVisibleMemorySize)*100)


    Write-Host "COMPUTER:$Server"`r`n

     Write-Host "CPU Usage:$Havecpu"`r`n
     Write-Host "Memory Usage:"
     Write-Host "Total Mem:$Allmem"
     Write-Host "Free Mem:$Freemem"
     Write-Host "Used Mem:$Permem"`r`n
     Write-Host "Disk Usage:"
     gwmi Win32_LogicalDisk -ComputerName $Server | Foreach-Object { 'Disk {0} has {1:0.0} GB space available' -f $_.Caption, ($_.FreeSpace / 1024MB) } 

    -----------------------------------------------------------------------------------------------

    actually I am not very familiar with powershell

    and this function just a simple function that I can get the server information without RDP

    Please help me to find out my wrong , thanks everyone



    • Edited by EasonChang Thursday, December 20, 2018 7:05 AM
    Thursday, December 20, 2018 7:00 AM

Answers

  • You have to add the credentials to the WMI commands.

    Get-WMIObject –computername $Server win32_Processor -Credential $Credential


    \_(ツ)_/

    • Marked as answer by EasonChang Thursday, December 20, 2018 7:51 AM
    Thursday, December 20, 2018 7:22 AM
  • Please format your code as code using the code posting tool provided. Thanks

    Yes, in each Get-WMIObject command. The last one is wrong. You have to move the parameter -Credential $Credential directly to the command bofore the pipe - not the Foreach-Object command.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, December 20, 2018 7:43 AM
    • Marked as answer by EasonChang Thursday, December 20, 2018 7:51 AM
    Thursday, December 20, 2018 7:43 AM

All replies

  • You have to add the credentials to the WMI commands.

    Get-WMIObject –computername $Server win32_Processor -Credential $Credential


    \_(ツ)_/

    • Marked as answer by EasonChang Thursday, December 20, 2018 7:51 AM
    Thursday, December 20, 2018 7:22 AM
  • in each Get-WMIObject command ?

    like:

    $cpu = Get-WMIObject –computername $Server win32_Processor -Credential $Credential
    $mem = gwmi -ComputerName $Server win32_OperatingSystem -Credential $Credential
    $Disks = gwmi –Computer: $Server win32_logicaldisk -filter "drivetype=3" -Credential $Credential

    gwmi Win32_LogicalDisk -ComputerName $Server | Foreach-Object { 'Disk {0} has {1:0.0} GB space available' -f $_.Caption, ($_.FreeSpace / 1024MB) } -Credential $Credential

    like this ?

    Thursday, December 20, 2018 7:39 AM
  • Please format your code as code using the code posting tool provided. Thanks

    Yes, in each Get-WMIObject command. The last one is wrong. You have to move the parameter -Credential $Credential directly to the command bofore the pipe - not the Foreach-Object command.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, December 20, 2018 7:43 AM
    • Marked as answer by EasonChang Thursday, December 20, 2018 7:51 AM
    Thursday, December 20, 2018 7:43 AM
  • thanks jrv and BOfH_66 it can work now

    now I think I need to solve the next step firewall problem.

    Really , many many thanks


    • Edited by EasonChang Thursday, December 20, 2018 7:54 AM
    Thursday, December 20, 2018 7:54 AM