KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Powershell script
Goto page 1, 2  Next
 
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting
View previous topic :: View next topic  
Author Message
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Thu Aug 24, 2023 2:18 pm    Post subject: Powershell script Reply with quote

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:
$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
}
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Thu Aug 24, 2023 11:11 pm    Post subject: Reply with quote

Quote:
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
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Fri Aug 25, 2023 7:16 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Fri Aug 25, 2023 7:35 am    Post subject: Reply with quote

Quote:
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
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?

Quote:
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.

Quote:
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
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Fri Aug 25, 2023 7:56 am    Post subject: Reply with quote

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

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?
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Fri Aug 25, 2023 1:34 pm    Post subject: Reply with quote

Quote:
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
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Fri Aug 25, 2023 1:44 pm    Post subject: Reply with quote

That's what I said it works.
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Fri Aug 25, 2023 1:52 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Fri Aug 25, 2023 2:52 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Sat Aug 26, 2023 7:17 am    Post subject: Reply with quote

Oh, I misunderstood 1st post
Quote:
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
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Mon Aug 28, 2023 8:39 am    Post subject: Reply with quote

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?!
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Mon Aug 28, 2023 11:00 am    Post subject: Reply with quote

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

Regards
Alex
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Mon Aug 28, 2023 2:09 pm    Post subject: Reply with quote

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:
$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:
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Mon Aug 28, 2023 2:48 pm    Post subject: Reply with quote

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

Invoke-Command -Computername computerName -ScriptBlock { ((Get-ChildItem FolderPath | Measure-Object -Property Length -sum).sum / 1MB)

Any error messages?
Regards
Alex
Back to top
View user's profile Send private message Visit poster's website
jcasares



Joined: 16 Sep 2008
Posts: 50

PostPosted: Mon Aug 28, 2023 2:57 pm    Post subject: Reply with quote

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):

Quote:
[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.

Quote:
[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
----------
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index