查询下载Microsoft电子书赠书的powershell脚本

查询下载Microsoft电子书赠书的powershell脚本,powershell,Powershell,收到whatsapp消息,微软将从下面的url免费赠送电子书 网址: 为了一次性下载所有书籍,使用了以下powershell脚本,该脚本位于同一url中 现在我的问题是,如果我整体运行powershell脚本,它不会抛出任何错误。url中的所有书籍都下载到我计算机中的一个位置 但是,如果我试图逐行运行脚本以了解每个语句的作用,则在执行$bookList=Invoke WebRequest$downLoadList时,会出现以下错误 现在要解决这个错误,堆栈溢出中还有许多其他帖子,它们传递用户名和

收到whatsapp消息,微软将从下面的url免费赠送电子书

网址:

为了一次性下载所有书籍,使用了以下powershell脚本,该脚本位于同一url中

现在我的问题是,如果我整体运行powershell脚本,它不会抛出任何错误。url中的所有书籍都下载到我计算机中的一个位置

但是,如果我试图逐行运行脚本以了解每个语句的作用,则在执行$bookList=Invoke WebRequest$downLoadList时,会出现以下错误

现在要解决这个错误,堆栈溢出中还有许多其他帖子,它们传递用户名和密码来克服这个错误。这些脚本/解决方案在我这边不起作用

除了错误之外,为什么在执行完整脚本时脚本运行时没有任何错误/问题,但在逐行执行时抛出错误

关于执行性质的任何输入或克服错误的有用提示都将非常有用。。。多谢各位

错误:

Invoke-WebRequest : (my ip number )
Credentials are missing.
Make sure to specify a domain with your username
This website has been blocked by a cyber security policy
and SecureWeb does not currently support web exceptions
If you have an exception, copy the link below into a new tab
http://ligman.me/2tk1D2V
At line:1 char:13
+ $bookList = Invoke-WebRequest $downLoadList
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) 
    [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands. 
   InvokeWebRequestCommand
############################################################### 
# Eric Ligmans Amazing Free Microsoft eBook Giveaway 
# https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepo/
# Link to download list of eBooks 
# http://ligman.me/2tk1D2V
# Thanks David Crosby for the template (https://social.technet.microsoft.com/profile/david%20crosby/)
#
# Modified by Robert Cain (http://arcanecode.me)
# Added code to check to see if a book was already downloaded,
# and if so was it the correct file size. If so, the book
# download is skipped. This allows users to simply rerun the
# script if their download process is interrupted. 
############################################################### 

# Set the folder where you want to save the books to
$dest = "I:\new_microsoft\" # Make sure the file path ends in a \

# Download the source list of books 
$downLoadList = "http://ligman.me/2tk1D2V" 
$bookList = Invoke-WebRequest $downLoadList 

# Convert the list to an array 
[string[]]$books = "" 
$books = $bookList.Content.Split("`n") 

# Remove the first line - it's not a book 
$books = $books[1..($books.Length -1)] 
$books # Here's the list 

# Get the total number of books we need to download
$bookCount = $($books).Count

# Set a simple counter to let the user know what book 
# number we're currently downloading
$currentBook = 0 

# As an option, we can have it log progress to a file
$log = $true

if ($log -eq $true)

{

  # Construct a log file name based on the date that
  # we can save progress to
  $dlStart = Get-Date
  $dlStartDate = "$($dlStart.Year)-$($dlStart.Month)-$($dlStart.Day)"
  $dlStartTime = "$($dlStart.Hour)-$($dlStart.Minute)-$($dlStart.Second)"
  $logFile = "$($dest)BookDlLog-$dlStartDate-$dlStartTime.txt"

}

# Download the books 
foreach ($book in $books) 

{ 

  # Increment current book number
  $currentBook++
  try

  {

    # Grab the header with the books full info
    $hdr = Invoke-WebRequest $book -Method Head 

    # Get the title of the book from the header then
    # make it a safe string (remove special characters)
    $title = $hdr.BaseResponse.ResponseUri.Segments[-1] 
    $title = [uri]::UnescapeDataString($title) 


    # Construct the path to save the file to
    $saveTo = $dest + $title 

    # If the file doesn't exist, download it
    if ($(Test-Path $saveTo) -eq $false)

    {

      $msg = "Downloading book $currentBook of $bookCount - $title"
      $msg
      if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
      Invoke-WebRequest $book -OutFile $saveTo 

    }

    else

    { 

      # If it does exist, we need to make sure it wasn't
      # a partial download. If the file size on the server
      # and the file size on local disk don't match, 
      # redownload it

      # Get the size of the file from the download site
      $dlSize = $hdr.BaseResponse.ContentLength
      # Get the size of the file on disk
      $fileSize = $(Get-ChildItem $saveTo).Length

      if ($dlSize -ne $fileSize)

      {

        # If not equal we need to download the book again
        $msg = "Redownloading book $currentBook of $bookCount - $title"
        $msg
        if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
        Invoke-WebRequest $book -OutFile $saveTo 

      }

      else

      {

        # Otherwise we have a good copy of the book, just
        # let the user know we're skipping it.
        $msg = "Book $currentBook of $bookCount ($title) already exists, skipping it"
        $msg
        if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }

      }

    }

  } # end try

  catch 

  {

    $msg = "There was an error downloading $title. You may wish to try to download this book manually." 
    Write-Host $msg -ForegroundColor Red
    if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }

  } # end catch

} # end foreach 


# Let user know we're done, and give a happy little beep 
# in case they aren't looking at the screen.
#"Done downloading all books"
#[Console]::Beep(500,300)
PowerShell脚本:

Invoke-WebRequest : (my ip number )
Credentials are missing.
Make sure to specify a domain with your username
This website has been blocked by a cyber security policy
and SecureWeb does not currently support web exceptions
If you have an exception, copy the link below into a new tab
http://ligman.me/2tk1D2V
At line:1 char:13
+ $bookList = Invoke-WebRequest $downLoadList
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) 
    [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands. 
   InvokeWebRequestCommand
############################################################### 
# Eric Ligmans Amazing Free Microsoft eBook Giveaway 
# https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepo/
# Link to download list of eBooks 
# http://ligman.me/2tk1D2V
# Thanks David Crosby for the template (https://social.technet.microsoft.com/profile/david%20crosby/)
#
# Modified by Robert Cain (http://arcanecode.me)
# Added code to check to see if a book was already downloaded,
# and if so was it the correct file size. If so, the book
# download is skipped. This allows users to simply rerun the
# script if their download process is interrupted. 
############################################################### 

# Set the folder where you want to save the books to
$dest = "I:\new_microsoft\" # Make sure the file path ends in a \

# Download the source list of books 
$downLoadList = "http://ligman.me/2tk1D2V" 
$bookList = Invoke-WebRequest $downLoadList 

# Convert the list to an array 
[string[]]$books = "" 
$books = $bookList.Content.Split("`n") 

# Remove the first line - it's not a book 
$books = $books[1..($books.Length -1)] 
$books # Here's the list 

# Get the total number of books we need to download
$bookCount = $($books).Count

# Set a simple counter to let the user know what book 
# number we're currently downloading
$currentBook = 0 

# As an option, we can have it log progress to a file
$log = $true

if ($log -eq $true)

{

  # Construct a log file name based on the date that
  # we can save progress to
  $dlStart = Get-Date
  $dlStartDate = "$($dlStart.Year)-$($dlStart.Month)-$($dlStart.Day)"
  $dlStartTime = "$($dlStart.Hour)-$($dlStart.Minute)-$($dlStart.Second)"
  $logFile = "$($dest)BookDlLog-$dlStartDate-$dlStartTime.txt"

}

# Download the books 
foreach ($book in $books) 

{ 

  # Increment current book number
  $currentBook++
  try

  {

    # Grab the header with the books full info
    $hdr = Invoke-WebRequest $book -Method Head 

    # Get the title of the book from the header then
    # make it a safe string (remove special characters)
    $title = $hdr.BaseResponse.ResponseUri.Segments[-1] 
    $title = [uri]::UnescapeDataString($title) 


    # Construct the path to save the file to
    $saveTo = $dest + $title 

    # If the file doesn't exist, download it
    if ($(Test-Path $saveTo) -eq $false)

    {

      $msg = "Downloading book $currentBook of $bookCount - $title"
      $msg
      if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
      Invoke-WebRequest $book -OutFile $saveTo 

    }

    else

    { 

      # If it does exist, we need to make sure it wasn't
      # a partial download. If the file size on the server
      # and the file size on local disk don't match, 
      # redownload it

      # Get the size of the file from the download site
      $dlSize = $hdr.BaseResponse.ContentLength
      # Get the size of the file on disk
      $fileSize = $(Get-ChildItem $saveTo).Length

      if ($dlSize -ne $fileSize)

      {

        # If not equal we need to download the book again
        $msg = "Redownloading book $currentBook of $bookCount - $title"
        $msg
        if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
        Invoke-WebRequest $book -OutFile $saveTo 

      }

      else

      {

        # Otherwise we have a good copy of the book, just
        # let the user know we're skipping it.
        $msg = "Book $currentBook of $bookCount ($title) already exists, skipping it"
        $msg
        if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }

      }

    }

  } # end try

  catch 

  {

    $msg = "There was an error downloading $title. You may wish to try to download this book manually." 
    Write-Host $msg -ForegroundColor Red
    if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }

  } # end catch

} # end foreach 


# Let user know we're done, and give a happy little beep 
# in case they aren't looking at the screen.
#"Done downloading all books"
#[Console]::Beep(500,300)

@4c74356b41当我逐行执行脚本时,以及当我执行这一行$bookList=Invoke WebRequest$downloadlist时,您是如何逐行执行它的?我把它扔到了ISE中,能够遍历它并运行
$bookList=Invoke WebRequest$downLoadList
很好,然后玩
$bookList
@Sambardo转到行并按F8。带有$dest和$downloadList的行在使用F8时工作正常。一旦我转到$bookList行并按F8键,它就会抛出问题中提到的错误。我只是在PowerShell ISE中执行的。还有一件事,我是在我的办公环境中这样做的,但仍然令人困惑的是,整个脚本运行没有任何问题,为什么它逐行失败。@4c74356b41当我逐行执行脚本时,以及当我执行这一行$bookList=Invoke WebRequest$downloadlist时,您是如何逐行执行它的?我把它扔到了ISE中,能够遍历它并运行
$bookList=Invoke WebRequest$downLoadList
很好,然后玩
$bookList
@Sambardo转到行并按F8。带有$dest和$downloadList的行在使用F8时工作正常。一旦我转到$bookList行并按F8键,它就会抛出问题中提到的错误。我只是在PowerShell ISE中执行的。还有一件事,我是在我的办公环境中做的,但仍然很困惑,整个脚本运行没有任何问题,为什么它一行一行地失败。