简体   繁体   English

目录创建失败并且没有错误

[英]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.这是问题所在。

  1. I get no errors.我没有错误。
  2. The directory is not always created`.并不总是创建目录`。
  3. When the primary script runs and attempts to export the csv it doesn't because the directory doesn't exist.当主脚本运行并尝试导出 csv 时它不会因为该目录不存在。 3a) if I create the directory everything works. 3a)如果我创建目录一切正常。

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.

相关问题 如何提高目录创建速度 - How to improve the speed of directory creation 在Active Directory中创建时禁用计算机对象 - Disabling Computer Object on Creation in Active Directory Move-Item失败,移动没有出现在目标目录中没有错误 - Move-Item Failure, move doesnt appear in destination directory no error 自动创建目录 Azure Data Lake Gen 2 - PowerShell - Creation of Directory Azure Data Lake Gen 2 Automatically - PowerShell Windows 2008 服务器 Powershell Active Directory -> 获取用户创建日期 - Windows 2008 server Powershell Active Directory -> Get user creation date Active Directory密码连接Azure自动化导致Azure SQL失败 - Active Directory Password Connection Azure SQL Failure from Azure Automation 使用PowerShell创建新邮箱期间设置Active Directory帐户属性 - Set the Active Directory account properties during a New-Mailbox creation with PowerShell powershell 脚本不工作,但没有显示错误。 查找目录中的所有 .xml 文件,对某些文本进行替换,移动到不同的目录 - powershell script isn't working, but shows no errors. Find all .xml files in a directory, do a replace on some text, move to different directory 子表达式失败 - Subexpression failure Powershell文件夹创建脚本 - Powershell folder creation script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM