Powershell神秘错误
请帮忙!我不知道该怎么办。 脚本按内容拆分文件:Powershell神秘错误,powershell,Powershell,请帮忙!我不知道该怎么办。 脚本按内容拆分文件: $InPC = "C:\Scripts\" Get-ChildItem $InPC -Filter *.prt | ForEach-Object -Process { $basename= $_.BaseName $m = ( ( Get-Content $_ | Where { $_ | Select-String "--------------------- Instance Type and Tr
$InPC = "C:\Scripts\"
Get-ChildItem $InPC -Filter *.prt | ForEach-Object -Process {
$basename= $_.BaseName
$m = ( ( Get-Content $_ | Where { $_ | Select-String "--------------------- Instance Type and Transmission --------------" -Quiet } | Measure-Object | ForEach-Object { $_.Count } ) -ge 2)
$a=1
if ($m) {
Get-Content $_ | % {
If ($_ -match "--------------------- Instance Type and Transmission --------------") {
$OutputFile = "$basename-$a.prt"
$a++
}
Add-Content $OutputFile $_
}
Remove-Item $_
}
}
当我将location设置为C:\Scripts时,一切都正常。但在基本情况下,它将不起作用,并给出下一个错误:
Get-Content : Path not found "C:\Users\a.ulianov\PRTPRT.prt".
C:\Scripts\2.ps1:23 знак:18
+ $m = ( ( Get-Content $_ | Where { $_ | Select-String "------------------ ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\a.ulianov\PRTPRT.prt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Path not found "C:\Users\a.ulianov\test.prt".
C:\Scripts\2.ps1:23 знак:18
+ $m = ( ( Get-Content $_ | Where { $_ | Select-String "------------------ ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\a.ulianov\test.prt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
它似乎在执行过程中使用默认的PS位置。在这种情况下,我可以修改什么
#
多亏了@Raf@Rynant,这里有一个小小的弯曲但有效的解决方案:
$InPC = "C:\Scripts"
Get-ChildItem -Path $InPC -Filter *.prt | ForEach-Object -Process {
$basename= $_.BaseName
$m = ( ( Get-Content $_.FullName | Where { $_ | Select-String "--------------------- Instance Type and Transmission --------------" -Quiet } | Measure-Object | ForEach-Object { $_.Count } ) -ge 2)
$a = 1
if ($m) {
Get-Content $_.FullName | % {
If ($_ -match "--------------------- Instance Type and Transmission --------------") {
$OutputFile = "$InPC\$basename _$a.prt"
$a++
}
Add-Content $OutputFile $_
}
Remove-Item $_.FullName
}
}
看起来有点凌乱,但我认为您需要做的唯一更改是更换:
获取内容$\u
与
Get Content$\ux.FullName
这不是一个真正的问题,但是$basename=%{$\uuu.basename}
应该是$basename=$\uuu.basename
谢谢,我更正了脚本这并没有起作用(((此时的结果是在C:\Windows\system32
创建了较新的拆分文件,并且删除项目也需要$。
。我不知道PS ISE到底是怎么回事,但启动时会随机选择默认位置,例如C:\Users\a.ulianov
,C:\Program files\Microsoft Office\Office14
等等。一个问题是,为什么它不能处理它们所在位置的文件?我修改了脚本,它看起来可能有点歪,但它可以工作。