我有一个PowerShell脚本,它检查它运行的服务器的CPU级别,然后如果它超过某个阈值,它将运行SQL存储过程和电子邮件.
该脚本在我的开发服务器上使用最新版本的PowerShell正确运行.但是,我Invoke-Sqlcmd在实时服务器上运行命令时遇到问题,我需要从中获取CPU值.为了举例,它可以称为LIVESERVER.它正在运行PowerShell 2.0,我认为是这个问题:
术语"Invoke-Sqlcmd"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
我宁愿不对此服务器进行任何更改,因为它对业务至关重要(除非这是一个简单的任务,无需重新启动即可更新PowerShell等).
是否可以从我的开发服务器运行我的脚本并指定服务器以获取CPU数据?我不确定语法如何实现这一点.
这是我的脚本:
# Email
$recipients = @("Ricky <ricky@email.com>")
# Loop 20 times
for ($i= 1; $i -le 20; $i++) {
# Find CPU average usage percentage for given time period
$cpuutil = (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 20 |
select -ExpandProperty countersamples |
select -ExpandProperty cookedvalue |
Measure-Object -Average).Average
# Display average CP output
$cpuutil
# If CPU average percentage is higher than given value, run stored procedure and
# …Run Code Online (Sandbox Code Playgroud) 我编写的脚本(应该)执行以下操作时遇到了持续的问题。我有一个文件夹,其中包含许多 csv 文件,我想将带有公司名称的最新文件复制到另一个文件夹中,并重命名它。
当前格式为:
21Feb17070051_CompanyName_Sent21022017
Run Code Online (Sandbox Code Playgroud)
我希望它采用以下格式:
CompanyName21022017
Run Code Online (Sandbox Code Playgroud)
所以我有以下 powershell 脚本来执行此操作:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy latest Company CSV file ##
get-childitem -path $csv_path -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 |
copy-item -Destination $csv_dest
## Rename the file that has been moved ##
get-childitem -path $csv_dest -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime | …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它通过服务器的文本文件并报告 25% 和 10% 以下的可用磁盘空间。然而,有些服务器有一个 P: 驱动器,用于分页文件,因此我希望循环忽略这个驱动器。
这是循环部分:
# Start processing disk space reports against a list of servers
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3" -EA SilentlyContinue
$computer = $computer.toupper()
foreach($disk in $disks)
{
$deviceID = $disk.DeviceID;
$volName = $disk.VolumeName;
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$usedSpaceGB = $sizeGB - $freeSpaceGB; …Run Code Online (Sandbox Code Playgroud)