我的剧本......
function DirSize {
BEGIN {}
PROCESS
{
$timer = Get-Date -UFormat "%Y%m%d%H%M%S"
$bFile="C:\00mpd\DR\mrv_incr_"+$timer+".txt"
$data = Get-Content "C:\00mpd\DR\vaults.txt"
write-host $data.count total lines read from file
foreach ($line in $data)
{
write-host $line
$a=0
foreach ($file in (Get-ChildItem $line -recurse -Force| where-object {!($_.psiscontainer)} | where { $_.LastWriteTime -ge ((get-date).AddHours(-1))} ))
{
$a+=$file.length/1GB
}
Write-Output "$line :: $a GB"
"$line :: $a GB"| Out-File $bFile -append
}
}
END {}
} # end function Get-DirSize
vaults.txt各种文件夹的内容名称....
d:\Milind_laptop_backup\DDrive\Milind_pst\
d:\Milind_laptop_backup\DDrive\Office\
d:\Milind_laptop_backup\DDrive\personal\
d:\Milind_laptop_backup\DDrive\ToMilind\
在powershell窗口中运行时,ps1以上会给出正确的结果。但是当我在dos shell中运行相同时,它没有给出任何结果。
Powershell命令
PS C:\> . "c:\00mpd\dr\dirsize.ps1"
PS C:\> DirSize
但是当我在Dos shell中尝试时,没有结果出来。
C:\00mpd\DR>powershell -File "c:\00mpd\dr\DirSize.ps1"
答案 0 :(得分:2)
您正在以脚本
运行脚本从PowerShell内部执行时,这是正常的,Function DirSize
加载它并DirSize
执行它。但是当从.ps1文件运行它时,它只是加载函数但不执行它。
解决这个问题:
方法1:
删除function DirSize {
和最后一行} # end function Get-DirSize
方法2:
Dot将.ps1作为加载函数的命令,然后在
之后运行它powershell -Command ". c:\00mpd\dr\DirSize.ps1; DirSize"
正如Mathias R. Jessen建议
答案 1 :(得分:-1)
正如其他人所说,如果你删除
function DirSize{ }
并点源您的脚本,它将解决一个问题。
我个人然后将Write-Output更改为Write-Host。它应该给出你正在寻找的结果。