我是PowerShell的新手,我一直在努力让这个脚本工作。
If ((Get-Date -UFormat %a) -eq "Mon") {$intSubtract = -3}
Else {$intSubtract = -1}
$datDate = (Get-Date).AddDays($intSubtract)
Write-Output "Find expected file --------------"
$strDate = ($datDate).ToString('yyyyMMdd')
Write-Host "strDate: $strDate"
$arrGetFile = Get-ChildItem -Path "\\Computer\Data\States\NorthDakota\Cities\*_Bismark_$strDate*.txt"
$strLocalFileName = $arrGetFile
If ($arrGetFile.count -ne 2)
{
Throw "No file or more than two files with today's date exists!"
}
Else {$strLocalFileName = $arrGetFile[0].Name}
Write-Output "Found file $strLocalFileName --------------"
#Encrypt each file
foreach ($arrGetFile in $strPath)
{
Write-Output "Start Encrypt --------------"
$strPath = "\\Computer\Data\States\NorthDakota\Cities\"
$FileAndPath = Join-Path $strPath $strLocalFileName
$Recipient = "0xA49B4B5D"
Import-Module \\JAMS\C$\PSM_PGP.psm1
Get-Module Encrypt
Encrypt $FileAndPath $Recipient
$strLocalFileNamePGP = $strLocalFileName + ".pgp"
Write-Output "End Encrypt --------------"
}
#Archive files
Write-Output "Archiving --------------"
move-item -path \\Computer\Data\States\NorthDakota\Cities\*_Bismark_$strDate*.txt -destination \\Computer\Data\States\NorthDakota\Cities\Archive“城市”文件夹将包含两个文件。示例2015_Bismark_20150626_183121.txt和2015_Bismark_20150626_183121_Control.txt
我试图使这两个文件加密,但它只是查找和加密的文件,没有_Control。它正在正确地归档两个文件。不确定我遗漏了什么也可以找到控制文件。
发布于 2015-06-26 16:57:18
您的for循环不正确。您有foreach ($arrGetFile in $strPath),但是$strPath在这一点上似乎不包含任何内容。
您的for循环应该是:
foreach ($LocalFile in $arrGetFile)您需要删除以下行:
$strLocalFileName = $arrGetFile这使得$strLocalFileName成为一个文件对象数组,但是在脚本的后面,您将它当作一个字符串来处理。您可能有更多的逻辑错误--您需要非常仔细地遍历脚本,并标识每个变量,并确保它包含您希望它包含的内容。
通常,您似乎将非字符串对象的数组视为字符串。注意,我将您的$strLocalFileName变量更改为$LocalFile。这是因为它是文件对象,而不是字符串对象。
下面是一个示例,它仅显示for循环遍历这两个文件。
If ((Get-Date -UFormat %a) -eq "Mon") {$intSubtract = -3}
Else {$intSubtract = -1}
$datDate = (Get-Date).AddDays($intSubtract)
Write-Output "Find expected file --------------"
$strDate = ($datDate).ToString('yyyyMMdd')
Write-Host "strDate: $strDate"
$arrGetFile = Get-ChildItem -Path "\\Computer\Data\States\NorthDakota\Cities\*_Bismark_$strDate*.txt"
If ($arrGetFile.count -ne 2)
{
Throw "No file or more than two files with today's date exists!"
}
Write-Output "Found files " ($arrGetFile | select Name | fl) "--------------"
#Process each file
foreach ($LocalFile in $arrGetFile)
{
$FileAndPath = Join-Path $LocalFile.DirectoryName $LocalFile
$FileAndPath
}从这个开始,然后小心地将您的加密处理添加回循环中。
另外,可以删除分配$FileAndPath的行。您可以在需要完整路径和文件名的任何地方使用$LocalFile.FullName。
https://stackoverflow.com/questions/31078043
复制相似问题