1

I'm working on setting up a script that can be run from any Windows servers to gather some perfmon scripts in the background in a rolling log file. I could use the PerfMon GUI, but I figured this would give me a good opportunity to learn some Powershell at the same time, and I could wrap it all in my own simplified GUI based on what my project specifically needs.

Basically, the script verifies that the ExecutionPolicy is set to remotesigned (so it can run), then defines a couple of variables for usage in the following commands, and outlines the counters to grab.

After that, it stores the command as a variable and then starts it as a job. The problem is that I'm never seeing the results of that job in the form of the .csv file I'm asking for. Here's the code:

Set-ExecutionPolicy remotesigned -Force $Computer = $env:COMPUTERNAME $1GBInBytes = 1GB $p = "\\$Computer\Process(System)\% Processor Time", "\\$Computer\PhysicalDisk(_Total)\Current Disk Queue Length", "\\$Computer\PhysicalDisk(0 c:)\% Disk Time", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Queue Length", "\\$Computer\PhysicalDisk(0 c:)\% Disk Read Time", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Read Queue Length", "\\$Computer\PhysicalDisk(0 c:)\% Disk Write Time", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Write Queue Length", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Transfer", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Read", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Write", "\\$Computer\PhysicalDisk(0 c:)\Disk Transfers/sec", "\\$Computer\PhysicalDisk(0 c:)\Disk Reads/sec", "\\$Computer\PhysicalDisk(0 c:)\Disk Writes/sec", "\\$Computer\PhysicalDisk(0 c:)\Disk Bytes/sec", "\\$Computer\PhysicalDisk(0 c:)\Disk Read Bytes/sec", "\\$Computer\PhysicalDisk(0 c:)\Disk Write Bytes/sec", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Transfer", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Read", "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Write", "\\$Computer\PhysicalDisk(0 c:)\% Idle Time", "\\$Computer\PhysicalDisk(0 c:)\Split IO/Sec"; $counter = {get-counter -counter $p -Continuous | Export-Counter C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes} Start-job $counter 

Any ideas? For right now, I'd like to kick off the job as a background job and I'll stop it via a separate powershell command. I just want it to roll all of that into a .csv though.

1 Answer 1

1

if you are using psv3, change as follows:

$counter = {get-counter -counter $using:p -Continuous | Export-Counter C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $using:1GBInBytes} 

otherwise you can't access $p and $1GBInBytes

if you are using v2:

$counter = {param($p, $1GBInBytes ) get-counter -counter $p -Continuous | Export-Counter C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes} Start-job $counter -ArgumentList $p, $1GBInBytes 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm using 3, I'll test out that solution and read up on $using some more. Is that a general rule, if I want to use a local variable I'll have to specify $using before the variable?
Yes, for invoke-command and start-job etc. you need to specify $using if you want to access local variable from scriptblock of that command, otherwise you need to use argumentlist

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.