Macauley
Macauley

Reputation: 399

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. 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

Upvotes: 0

Views: 2229

Answers (1)

Macauley
Macauley

Reputation: 399

Okay so credit to this goes to Micky Balladelli for his much appreciated help! 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

$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

Upvotes: 2

Related Questions