英文:
how to solve about out-file in powershell
问题
I wrote a PowerShell script it was working (i am using windows10 64bit) but suddenly it stopped.
Here is my script:
Try{
error.clear();
write-host "After Clear Error";
write-host "Location is: " -nonewline;
(get-location).Path
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Data.SqlClient");
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO");
Write-host "After setting systems for forms";
[string] $CRLF = "`r`n"
[string] $fileName = "civ_" + (get-date).ToString("yyyyMMdd_HHmmss") + " .txt";
Write-host "File Name is: $fileName";
[string] $outputFile = ("C:\temp\" + $fileName);
write-host "outputfile is: $outputFile";
[string] $newLocation = "G:\Teams\Data\support\";
Write-host "After setting file information";
($CRLF + "Script Run Commencing at " + (get-date).ToString("yyyyMMdd_HH:mm:ss.fff")) | out-file $outputFile -append -encoding ASCII;
write-host "After writing information to file";
The error message is:
> out-file: Could not find file 'C:\temp\civ_20230224_105513.txt'
I have checked several times, not sure where did it go wrong.
So I turn to the almighty community.
Thank you.
英文:
I wrote a PowerShell script it was working (i am using windows10 64bit)
but suddenly it stopped.
Here is my script:
Try{
error.clear();
write-host "After Clear Error";
write-host "Location is: -nonewline;
(get-location).Path
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Data.SqClient");
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO");
Write-host "After setting systems for forms";
[string] $CRLF = "`r`n"
[string] $fileName = "civ_" + (get-date).ToString("yyyyMMdd_HHmmss") + " .txt";
Write-host "File Name is: $fileName";
[string] $outputFile = ("C:\temp\" + $fileName);
write-host "outputfiIe is: SoutputFiIe";
[string] $newLocation = "G:\Teams\Data\support\";
Write-host "After setting FILE information";
($CRLF + "Script Run Commencing at " + (get-date).ToString("yyyyMMdd_HH:mm:ss.fff")) | out-file $outputFile -append -encoding ASCII;
write-host "After WRITING INFORMATION TO FILE';
The error message is
> out-file: Couldnot find file 'C:\temp\civ_20230224_105513.txt'
I have checked several times, not sure where did it go wrong.
So I turn to the almighty community.
Thank you
答案1
得分: 1
抱歉说,你上面的代码充满了语法错误,例如不匹配的引号和应该在引号外部的参数。你还试图使用过时的方法加载程序集和不正确的程序集名称,或者至少是我没有加载的程序集,所以我将它们注释掉了。
以下是一个可用于比较差异的工作版本:
Clear-Host
$error.clear()
write-host "After Clear Error"
write-host "Location is: $((get-location).Path)"
Add-Type -assemblyname "System.Windows.Forms"
# 下面两行被注释掉,因为找不到程序集,可能会触发丢失Catch?
# Add-Type -assemblyname "System.Data.SqlClient"
# Add-Type -assemblyname "Microsoft.SqlServer.SMO"
Write-host "After setting systems for forms"
$fileName = "civ_" + $((get-date).ToString("yyyyMMdd_HHmmss")) + ".txt"
Write-host "File Name is: $fileName"
$outputFile = $(Join-Path -path "C:\temp" -ChildPath "$fileName")
write-host "outputFile is: $outputFile"
$newLocation = "G:\Teams\Data\support\"
Write-host "After setting FILE information"
$("`n$CRLF Script Run Commencing at $((get-date).ToString("yyyyMMdd_HH:mm:ss.fff"))") |
out-file $outputFile -append -encoding ASCII
write-host "After WRITING INFORMATION TO FILE"
以下是屏幕输出:
After Clear Error
Location is: G:\BEKDocs\Scripts
After setting systems for forms
File Name is: civ_20230223_210905.txt
outputFile is: C:\temp\civ_20230223_210905.txt
After setting FILE information
After WRITING INFORMATION TO FILE
以下是文件内容:
Script Run Commencing at 20230223_21:09:05.208
如果你没有使用像ISE或Visual Code这样的工具,你应该使用它们,因为它们可以捕捉到你的许多问题。
希望这对你在PowerShell之旅中有所帮助。
英文:
I'm sorry to say that your code above is riddled with syntactical errors, .e.g. quotes that are mismatched and arguments within quotes that should be outside them. You are also trying to load assemblies with outdated methods and incorrect assembly names or at least ones I didn't have loaded so I commented them out.
Here is a working version which you can use to compare the differences with.
Clear-Host
$error.clear()
write-host "After Clear Error"
write-host "Location is: $((get-location).Path)"
Add-Type -assemblyname "System.Windows.Forms"
#Next two comment out can't find assembly tripping the missing Catch?
#Add-Type -assemblyname "System.Data.SqClient"
#Add-Type -assemblyname "Microsoft.SqlServer.SMO"
Write-host "After setting systems for forms"
$fileName = "civ_" + $((get-date).ToString("yyyyMMdd_HHmmss")) + ".txt"
Write-host "File Name is: $fileName"
$outputFile =$(Join-Path -path "C:\temp" -ChildPath "$fileName")
write-host "outputfiIe is: $outputFile"
$newLocation = "G:\Teams\Data\support\"
Write-host "After setting FILE information"
$("`n$CRLF Script Run Commencing at $((get-date).ToString("yyyyMMdd_HH:mm:ss.fff"))") |
out-file $outputFile -append -encoding ASCII
write-host "After WRITING INFORMATION TO FILE"
Here's the Screen Output:
After Clear Error
Location is: G:\BEKDocs\Scripts
After setting systems for forms
File Name is: civ_20230223_210905.txt
outputfiIe is: C:\temp\civ_20230223_210905.txt
After setting FILE information
After WRITING INFORMATION TO FILE
Here's the file content:
Script Run Commencing at 20230223_21:09:05.208
If you are not using a tool like the ISE or Visual Code you should be as they would have caught a lot of your problems.
I hope this helps you along your PowerShell journey.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论