[英]Directory creation failure and no errors
I made a function and I thought it worked well.我做了一个 function,我认为它运作良好。 In my primary script I am importing the function and then calling it.
在我的主脚本中,我正在导入 function 然后调用它。 The idea of the function is simply to make a filename with a path.
function 的想法很简单,就是用路径创建一个文件名。 At the end of the function I added a portion that would check if the directory exists and if not create it.
在 function 的末尾,我添加了一个部分来检查目录是否存在,如果不存在则创建它。
Here are the problems.这是问题所在。
The folder it tries to create is "C:\Reports" but I am an admin on the computer and I understand it should be in special folders.它尝试创建的文件夹是“C:\Reports”,但我是计算机上的管理员,我知道它应该位于特殊文件夹中。 I suppose I can force that if you folks want.
我想如果你们愿意,我可以强制执行。 But I would think I would get an error if the directory could not create as written below.
但我认为如果目录无法按如下所述创建,我会收到错误消息。
Portion of the function that should be working.应该工作的 function 的一部分。
$ProcessError = $null
IF (!(test-path $DirectoryName)) {
New-Item -Path $DirectoryName -ItemType Directory -ErrorAction SilentlyContinue -ErrorVariable ProcessError -Force
If ($ProcessError) {
Write-Warning "Problem trying to create $DirectoryName."
}
Return [string]$FullPath
}
This is the primary call to the function and what is strange is I get no error here as well in section 2 primary below!!这是对 function 的主要调用,奇怪的是我在下面的第 2 部分中也没有错误!!
section 1 primary第 1 节小学
If ($SimChange -eq $True) {
$ResultFileName = MRNAP -ReportName "ExampleGoogleLicenseReport" -Move -JustDate
}
Else {
$ResultFileName = MRNAP -ReportName "GoogleLicenseReport" -Move -JustDate
}
Section 2 primary第 2 节初级
$ProcessError = $null
$Results | Export-Csv $ResultFileName -NoTypeInformation -ErrorVariable ProcessError -ErrorAction SilentlyContinue
If ($ProcessError) {
Write-Warning "Unable to save report output to $ResultFileName"
}
url to function that creates the filename.创建文件名的 url 到 function。 https://github.com/dcazman/MRNAP/blob/main/MRNAP.ps1
https://github.com/dcazman/MRNAP/blob/main/MRNAP.ps1
I find this all very frustrating and Section 2 primary above.我觉得这一切都非常令人沮丧,并且上面的第 2 节主要内容。 Should be giving me that warning as written....I thought.
应该给我书面的警告......我想。
In the comments there is chat about a bug and using try catch.在评论中有关于错误和使用 try catch 的聊天。 Additionally, we have talked about this problem on stack before with Create directory if it does not exist and Function return value in PowerShell
此外,我们之前已经在堆栈上讨论过这个问题, 如果目录不存在则创建目录, Function 在 PowerShell 中返回值
What I did for now that works.我现在所做的工作。
IF (!(test-path $DirectoryName)) {
$Dircreate = New-Item -ItemType Directory -Force -Path $($DirectoryName.Substring(0, 3)) -Name $($DirectoryName.Substring(3)) -ErrorAction SilentlyContinue -ErrorVariable ProcessError | Out-Null
If ($ProcessError) {
Write-Warning "Problem trying to create $DirectoryName."
[bool]$Created = $False
}
Else {
[bool]$Created = $true
}
}
If ($Created) {
If ($FullPath -as [string]) {
Return [string]$FullPath
}
Else {
Return [string]$FullPath[1]
}
}
This above doesn't use a try catch but I will give that a shot later.上面的这个没有使用 try catch,但我稍后会试一试。 For some reason the creating the directory was changing
$fullpath
from a string to an array and that is why the test above works using the -as [string]
由于某种原因,创建目录将
$fullpath
从字符串更改为数组,这就是为什么上面的测试使用-as [string]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.