简体   繁体   English

访问路径被拒绝Powershell

[英]Access to Path is Denied Powershell

right now I have just set my environment variable and am trying to have the file output to the Temp directory. 现在,我刚刚设置了环境变量,并试图将文件输出到Temp目录。 Every time I run the batch file though it says that access to pathway is denied, below is the entirety of my code 每次我运行批处理文件时,尽管它说拒绝访问路径,以下是我的全部代码

Param(
    [Parameter(Mandatory=$true,
         ValueFromRemainingArguments=$true,
         HelpMessage="List of files to add to the package")]
[String] $File

)

Function CreateNugetPackage {
    Param(
        [String] $File
    )
    Process {
        $retCode = 0
        write-verbose "Getting ID, Version, and Filepath of $file..."
        $version = (Get-Item $File).VersionInfo.FileVersion
        $id = $file.Substring(0, $File.LastIndexof('.'))
        $filepath = Get-ChildItem "$File"
        $netVer = ildasm /text $File| findstr Metadata
        $netVerShort = $netVer.Substring(0, $netVer.IndexOf('.') + 1 +   $netVer.Substring($netVer.IndexOf('.') + 1).IndexOf('.'))
        $netVerConv = @{
        'v2.0' = "lib\net20";
        '// Metadata version: v2.0' = "lib\net20";
        'v3.0' = "lib\net30";
        'v3.5' = "lib\net35";
        'v4.0' = "lib\net40";
        'v4.5' = "lib\net45";
        }
        $target = $netVerConv.Get_Item($netVerShort)
        $OriginalFilename = (Get-Item $File).VersionInfo.OriginalFilename
        write-verbose "$id"
        write-verbose "$version"
        write-verbose "$filepath"
        write-verbose "$netVer"
        write-verbose "$netVerShort"
        write-verbose "$target"
        function CreateNewNuspec { 
            param ($File)
            $x=
           "<?xml version=""1.0""?>
            <package xmlns=""http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"">
            <metadata>
                <id>$id</id>
                <version>$version</version>
                <authors>CME</authors>
                <owners>CME</owners>
                <requireLicenseAcceptance>false</requireLicenseAcceptance>
                <description>$filepath</description>
            </metadata>
            <files>
                <file src=""$id.dll"" target=""$target""/>
            </files>
            </package>"
            return $x
        }
        CreateNewNuspec > $env:temp "$id-$version.xml"

        Return $retCode
    }
}

$retVal = CreateNugetPackage $File
exit $retVal
}

It's not the cleanest code but this is my first time coding in powershell 这不是最干净的代码,但这是我第一次在Powershell中进行编码

Okay so credit to this goes to Micky Balladelli for his much appreciated help! 好的,这归功于Micky Balladelli的大力帮助! He suggested 他建议

$env:temp+"\"+"$id-$version.xml"

but that didn't work. 但这没用。 What did work though was placing that within a variable, so I made a variable to hold what Micky suggested 虽然有效的方法是将其放在变量中,所以我制作了一个变量以保存Micky的建议

$OutputFile = $env:temp+"\"+"$id-$version.xml"

and at the bottom of the code I called the function 在代码的底部,我调用了该函数

CreateNewNuspec > $OutputFile

and that resolved the issue entirely 这完全解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM