[英]Why Get-Date throws Index was outside the bounds of the array error?
I am working on a PowerShell script that searches the computer for a certain string and puts the results in a array. 我正在使用PowerShell脚本,该脚本在计算机上搜索某个字符串并将结果放入数组中。 When it is done, it prints the results of the array into a log file.
完成后,它将阵列的结果打印到日志文件中。 The issue I am having is naming the file.
我遇到的问题是文件命名。 I would like to have the name say "AAV- date .log".
我想使用“ AAV- date .log”作为名称。 Where date is the current date and time in this format: YmdHMS.
其中date是当前日期和时间,格式为:YmdHMS。
The way it gets the date is by using Get-Date -UFormat $Y$m$d$H$M$S
and stores in a variable, and looks like $Date = Get-Date -UFormat $Y$m$d$H$M$S
and then $Date
is called for when the log file is created. 它获取日期的方式是使用
Get-Date -UFormat $Y$m$d$H$M$S
并将其存储在变量中,看起来像$Date = Get-Date -UFormat $Y$m$d$H$M$S
创建日志文件时先调用$Date = Get-Date -UFormat $Y$m$d$H$M$S
,然后调用$Date
。 The issue is, when it runs, it throws an error saying "Index was outside the bounds of the array". 问题是,它在运行时会引发错误,提示“索引在数组的边界之外”。 I have tried the Get-Date line by itself and still get the error.
我已经尝试过Get-Date行,但仍然收到错误。 This is error message in its fullness:
这是完整的错误消息:
Get-Date : Index was outside the bounds of the array.
At C:\Users\*******\Documents\*******\*********\*****\scanner.ps1:4 char:9
+ $Date = Get-Date -UFormat $Y$m$d$H$M$S
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Date], IndexOutOfRangeExceptio
n
+ FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.
Commands.GetDateCommand
I have tried Google-ing it but with nothing related to Get-Date in PowerShell. 我已经尝试使用Google-ing,但是在PowerShell中与Get-Date没有任何关系。 Any ideas as of why it is throwing this error and what I am doing wrong along with how to fix it?
关于它为什么会引发此错误以及我在做什么错误以及如何解决该错误的任何想法? Thanks in advance!
提前致谢!
The -Uformat
is looking for characters with preceding percent signs. -Uformat
正在寻找前面带有百分号的字符。 Not dollars signs which denotes variable names in PowerShell 不是美元符号,它表示PowerShell中的变量名称
Get-Date -UFormat "%Y%m%d%H%M%S"
You were sending it a blank string since those variables are not declared. 您正在向其发送空白字符串,因为未声明这些变量。 You would get the same error if you ran this as well
如果您也执行此操作,将会得到相同的错误
Get-Date -UFormat ""
There is more detail at docs.microsoft.com docs.microsoft.com上有更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.