【问题标题】:Looping functionality in Powershell for each folder inside System.DefaultWorkingDirectoryPowershell 中针对 System.DefaultWorkingDirectory 中的每个文件夹的循环功能
【发布时间】:2021-03-17 08:08:19
【问题描述】:

以下是我的 PowerShell 脚本,它在 Azure DevOps 中创建我的 DefaultWorkingDirectory 的 zip 文件,并成功地将其上传到 SharePoint 中,供我的最终用户下载。我想为我的目录创建一个循环,并在 SharePoint 中上传每个文件夹,并为每个文件夹创建一个单独的链接,而不仅仅是我现在做的父目录......希望在 PowerShell 中创建这个 for-each 功能时得到一些帮助:

这是我现有的代码:

    Function UPLOAD-FILE
{
    param($workingDir, $tempDir, $clientId, $clientSecret, $artifactname)
    CREATE-ARCHIVE -workingDir $workingDir -tempDir $tempDir    
    Write-Host $workingDir    
    write-host "$SPid"
    write-host "$LibId"
    $file = $workingDir + "\" +$tempDir + "\" + "$($artifactname).zip"
    $fileSize = (Get-Item $file).length
    $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -artifactname $artifactname
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadHeaders.Add("Content-Type", "application/json")
    $uploadHeaders.Add("Content-Range", "bytes " + 0 +"-" + ($fileSize-1) + "/" + $fileSize)
    $uploadHeaders.Add("Content-Length", $fileSize)
    $uploadHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)    
    $uploadBody = [System.IO.File]::ReadAllBytes($file)
    $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
    $response | ConvertTo-Json
    REMOVE-TEMPDIR -workingDir $workingDirectory -tempDir $tempDirectory
    return $response
    
}

Function GET-UPLOADLINK
{
    param($clientId, $clientSecret, $artifactname)
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadLinkRequestHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadLinkRequestHeaders.Add("Content-Type", "application/json")
    $uploadLinkRequestHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)
    $uploadLinkRequestBody = ""     
    #update the actual link  
    $fullname = "https://graph.microsoft.com/v1.0/sites/$($SPid)/drives/$($LibId)/root/children/"    
    $comname = $fullname + $artifactname   
    $uploadLinkResponse = Invoke-RestMethod "$($comname).zip/createUploadSession" -Method 'POST' -Headers $uploadLinkRequestHeaders -Body $uploadLinkRequestBody
    $uploadLinkResponse | ConvertTo-Json
    return $uploadLinkResponse    
}

Function CREATE-ARCHIVE
{
    param($workingDir, $tempDir)
    write-host $workingDir
    cd $workingDir
    md $tempDir
    Compress-Archive -Path $workingDir -DestinationPath $workingDir\$tempDir\$artifactname
}

Function REMOVE-TEMPDIR
{
    param($workingDir, $tempDir)
    #This will be the last step
    rm $workingDir\$tempDir -Recurse   
}



UPLOAD-FILE -workingDir $workingDirectory -tempDir $tempDirectory -clientId $CLIENT_ID -clientSecret $CLIENT_SECRET -artifactname $artifactname

这里我的 $workingDirectory 是 $(System.DefaultWorkingDirectory) 定义为在发布管道中调用的 powershell 任务中的参数 $tempDir 是“temp”,而 $artifactname 是一个自定义名称,只是为了识别我的目录。其余变量是不言自明的。

【问题讨论】:

    标签: powershell azure-devops


    【解决方案1】:

    您可以通过ls 命令或Get-ChildItem 命令来获取文件夹$(System.DefaultWorkingDirectory) 下的子文件夹,见下例:

    $items = Get-ChildItem $(System.DefaultWorkingDirectory)
    #or using ls command
    #$items  = ls $(System.DefaultWorkingDirectory)
    
    foreach($item in $items){
       $name = $item.Name
       $fullName = $item.FullName
       #if the path is a folder. Then create a zip
       if(Test-Path -Path $fullName -PathType Container){
          Compress-Archive -Path $fullName -DestinationPath "$name.zip"
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2022-11-02
      • 2013-07-08
      • 1970-01-01
      相关资源
      最近更新 更多