我正在尝试获取远程计算机路径上的文件夹结构和每个文件夹大小。我能够获取文件夹结构,但无论我指定的任何文件夹路径如何,文件夹大小都会返回 901.00 KB 的标准数字。我可以看到脚本能够正确获取远程文件夹路径,但不能正确获取文件/文件夹大小。
这是我到现在为止想出的代码。
param(
[Parameter(ValueFromPipeline=$True, Mandatory=$True)]
[System.String] $ComputerName,
[Parameter(ValueFromPipeline=$True, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.String] $Path
)
function Get-FolderSize ($_ = (get-item .)) {
Process {
$RemoteServer = @($ComputerName)
ForEach($Computer in $ComputerName) {
$ErrorActionPreference = "SilentlyContinue"
$length = Invoke-Command -ComputerName $ComputerName { (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
} -ArgumentList $Path
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Folder ($_.FullName)
$obj | Add-Member NoteProperty Length ($length)
Write-Output $obj
}
}
}
Function Class-Size($size)
{
IF($size -ge 1GB) …Run Code Online (Sandbox Code Playgroud) 我试图在我的脚本中使用下面的代码片段检索远程服务器的UPTIME.
$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime
$sysuptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
$uptime = " UPTIME : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds"
Run Code Online (Sandbox Code Playgroud)
执行脚本时出现以下错误:
Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.
Parameter name: dmtfDate"
Run Code Online (Sandbox Code Playgroud)
我无法确定错误消息是什么参数是必需的?
谢谢!
powershell ×2