Powershell script

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Powershell script

Post by jcasares »

I'm trying to get a folder size through PowerShell instead of internal test because it's across a slow link. But this is not working with HostMonitor.

Can someone tell me why the result is 0?
When I run the script on the server with the RMA agent, it replies properly (135). If I run through HM that execute it on a Windows RMA agent, I get 0.

Script:

Code: Select all

$statusUnknown = "ScriptRes:Unknown:"
$statusOk = "ScriptRes:Ok:"
$statusBad = "ScriptRes:Bad:"

$computer = $args[0]
$folder = $args[1]
$size = $args[2]

if (!$computer) {
  echo  $statusUnknown"Hostname not specified"
  exit
} elseif (!$folder) {
  echo  $statusUnknown"Folder not specified"
  exit
} elseif (!$size) {
  echo  $statusUnknown"Max size not specified"
  exit
}

$result = Invoke-Command -Computername $computer -ScriptBlock { "{0:0}" -f ((Get-ChildItem $folder -Recurse | Measure-Object -Property Length -sum).sum / 1MB) }

if ($result -gt $size) {
  echo $statusBad$result
} else {
  echo $statusOk$result
}
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

folder size across a slow link.
The best solution would be to use RMA agent on remote system and built-in Folder/File Size test method.

Perhaps your script does not work for the same reason - slow link?
or HostMonitor is started under account that does not have access to target resource

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

The remote RMA agent is running with a domain admin account, that does have access to the remote system. That system is over a slow WAN, and we tried the built-in test, but it ends failing. The PowerShell script that we run on the server with the RMA Agent and pointing to the other remote server gives the proper response. It is when is run inside HM that it returns 0.

The PowerShell is invoking a remote run, so the script runs locally on the remote system speeding up the directory size calculation and then returns the result to the one where the script was run (Invoke-Command).

EDIT: I get you mean to deploy extra agents in each remote system instead of having just one in a domain server. That would increase costs of the solution without a real need.
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

The remote RMA agent is running with a domain admin account, that does have access to the remote system. That system is over a slow WAN, and we tried the built-in test, but it ends failing.
You installed RMA somewhere beside HostMonitor and using it to check some other system over slow link? It does not make sense :roll:
If you have problem with connection, then RMA should be installed on other side of that slow connection.
HostMonitor -> slow connection -> RMA on target system (or near target system, same LAN, same domain) = good idea
HostMonitor -> RMA -> slow connection -> target system = bad idea

What exactly means "it ends failing"? Test status? Reply?
The PowerShell script that we run on the server with the RMA Agent and pointing to the other remote server gives the proper response. It is when is run inside HM that it returns 0
Permission issue?
HostMonitor does not execute scripts, Windows does. So if you use the same account for HostMonitor and RMA, results should be the same.
I get you mean to deploy extra agents in each remote system instead of having just one in a domain server. That would increase costs of the solution without a real need.
Single RMA can monitor many systems so you can check all systems on another end of the link using single RMA.
Unless you have many different slow links?

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

KS-Soft wrote:You installed RMA somewhere beside HostMonitor and using it to check some other system over slow link? It does not make sense :roll:
Installation is HostMonitor > slow link > RMA > slow link > remote system (two jumps)
KS-Soft wrote:What exactly means "it ends failing"? Test status? Reply?
Reply value is returning 0 instead of the proper value (i.e. 135 MB)
KS-Soft wrote:HostMonitor does not execute scripts, Windows does. So if you use the same account for HostMonitor and RMA, results should be the same.
RMA agent is running with a domain account that has rights over remote system. That is how is working in all other RMA agents. Added complexity is that we have two slow links jumps.
KS-Soft wrote:Single RMA can monitor many systems so you can check all systems on another end of the link using single RMA.
You're right, we might want to deploy a single agent on that remote network to reach all servers there. Small investment and might resolve the issue to use local test. I do still think it is more performant to run the PowerShell remote scripting. Do you have any tested scenario with Inkvoke-Command in HM?
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Do you have any tested scenario with Inkvoke-Command in HM?
Does not work on our systems. May be SPN records should be set...
Try to start script from command line on HostMonitor system and check for error message.

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

That's what I said it works.
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

It works when started on RMA system.
And it works on HostMonitor system started from command line?
Then use the same user account for HostMonitor.

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

I understand your last question now.

HostMonitor > RMA Agent > Remote System

No, we can not try it on HostMonitor as it's not in the same domain as RMA, but RMA is on the same domain as remote system and in there the script works.

HostMonitor has no visibility on the remote system (different unreachable network), that why we use remote RMA Agent.

Regards.
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Oh, I misunderstood 1st post :oops:
When I run the script on the server with the RMA agent, it replies properly (135). If I run through HM that execute it on a Windows RMA agent, I get 0.
So
(I) script started from command line on RMA system (systemB) works fine
(II) script started by RMA agent on the same systemB does not work

Could you try to start RMA using the same account (the one you are using for command line)?

Could you try to start script from command line using 32bit version of powershell?
C:\Windows\syswow64\windowspowershell\v1.0\powershell.exe

and/or start script from RMA using 64bit powershell?
C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

I tried changing the service account for the agent to the exact same as I was running the script, but the reply is still the same (0). I also tried forcing 32 or 64 bit PowerShell locations, but both returned 0.

I do suppose that the invoke command has some issue when the reply returns to the executed script by RMA agent. Or maybe there is some mistake on the code?!
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Script looks fine.
RMA started as service? Can you try application mode?
May be modify script using hardcoded username and password?

Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

Tried in application mode but it's the same.

Tried adding credentials to the Invoke-Command, but it fails inside RMA Agent (not if I run in PowerShell).

Code: Select all

$statusUnknown = "ScriptRes:Unknown:"
$statusOk      = "ScriptRes:Ok:"
$statusBad     = "ScriptRes:Bad:"

$computer = $args[0]
$folder = $args[1]
$size = $args[2]

if (!$computer) {
  echo  $statusUnknown"Hostname not specified"
  exit
} elseif (!$folder) {
  echo  $statusUnknown"Folder not specified"
  exit
} elseif (!$size) {
  echo  $statusUnknown"Max size not specified"
  exit
}

$cred=import-clixml C:\Temp\credfile.xml
$result = Invoke-Command -Computername $computer -ScriptBlock { "{0:0}" -f ((Get-ChildItem $folder -Recurse | Measure-Object -Property Length -sum).sum / 1MB) } -credential $cred

if ($result -gt $size) {
  echo $statusBad$result
} else {
  echo $statusOk$result
}
If I run in PowerShell it works:
Image
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Could you try HostMonitor Shell Script Manager -> "Lets try" page, set agent and simpler script with command like

Code: Select all

Invoke-Command -Computername computerName -ScriptBlock { ((Get-ChildItem FolderPath | Measure-Object -Property Length -sum).sum / 1MB) 
Any error messages?
Regards
Alex
jcasares
Posts: 50
Joined: Tue Sep 16, 2008 9:33 am

Post by jcasares »

Let's see if I got it.

You mean, I create a test script with only one line and static values. Then hit Let's Try with the specific agent in script manager (that's what I use to test it all the time). This is the result after 10 seconds (timeout setting):
[17:51:54] Agent: XXX is going to execute "Folder Size (TEST)" script ...
[17:52:05] Response from the agent: 301 - Error: script timed out
But if I use the full script, it replies faster (half the time) and with a 0 value.
[17:55:17] Agent: XXX is going to execute "Folder Size (MB)" script ...
[17:55:21] Script executed, correct result received:
----------
- Status: Ok
- Reply: 0
----------
Post Reply