0

Textbook: Windows PowerShell in Action (E3, 2017) - pp. 2 and 3.

Executing the below three blocks of code below produces the subsequent three errors, respectively.

I recognise this is likely a straightfoward resolution, however am a newcomer to PowerShell and my spending significant time attempting to understand why introductory examples do not work at this premature stage of learning is a frustration.

Thank you in advance if you can assist.

More details available on request.

Code block #1:

Get-ChildItem -Path $env::windir\*.log |
Select-String -List error |
Format-Table Path,LineNumber -AutoSize

Code block #1 - Error:

Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:1 char:1
+ Get-ChildItem -Path $env::windir\*.log |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Windows\system32:String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

Code block #2:

([xml] [System.Net.WebClient]::new().
DownloadString('https://blogs.msdn.microsoft.com/powershell/feed.aspx')).
RSS.Channel.Item |
Format-Table table,link

Code block #2 - Error:

Method invocation failed because [System.Net.WebClient] does not contain 
a method named 'new'.
At line:1 char:1
+ ([xml] [System.Net.WebClient]::new().
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Code block #3:

using namespace System.Windows.Forms
$Form = [Form] @{
Text = 'My First Form'
}
$button = [Button] @{
Text = 'Push Me!'
Dock = 'Fill'
}
$button.add_click{
$form.Close()
}
$form.Controls.Add($button)
$form.ShowDialog()

Code block #3 - Error:

At line:1 char:1
+ using assembly System.Windows.Forms
+ ~~~~~
The 'using' keyword is not supported in this version of the language.
At line:2 char:1
+ using namespace System.Windows.Forms
+ ~~~~~
The 'using' keyword is not supported in this version of the language.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ReservedKeywordNotAllowed
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 2
    That book likely has a presumption of what version of PowerShell you are supposed to be learning... .What version of PowerShell do you have ? `Get-host`. That would explain 2 and 3. 1. is just a typo `Get-ChildItem -Path $env::windir\*.log` should be `Get-ChildItem -Path $env:windir\*.log` – Matt Dec 06 '18 at 17:03
  • Have you researched any of these? I'm no powershell guru by any means (I know about enough to get myself into trouble), but they seem pretty straightforward to research or resolve, especially the third one. – Becuzz Dec 06 '18 at 17:05
  • What about going online and download the [code from manning.com](https://manning-content.s3.amazonaws.com/download/8/d03f562-787f-4511-a140-9f43b507893a/PowerShellinAction3eCode.zip) you'll see revised code without unintended line breaks. –  Dec 06 '18 at 17:09
  • Code block #1: Resolved. Thanks, Matt. 'Get-Host' output: version 5.1.17134.407. LotPings: Code without line breaks appears not to make a difference. Code blocks #2 and #3 still unresolved. – theemperor Dec 06 '18 at 17:20
  • They do work here on win10pro/PSv5.1, #1 only one colon, #2 only 2 lines, #3 1st line `using assembly System.Windows.Forms` missing –  Dec 06 '18 at 17:23
  • Correction regarding code block #3: Accounting for line breaks does indeed make a difference for this code. Thanks, LotPings. Resolved. Code block #2 unresolved at present. – theemperor Dec 06 '18 at 17:26
  • I don't have the book, but there should be information which PowerShell version is required. #2,#3 fail on PSv2 –  Dec 06 '18 at 17:29
  • 1
    LotPings: Book published in 2017 and in light of v5. – theemperor Dec 06 '18 at 17:31
  • Resolved #2: https://stackoverflow.com/questions/29942729/powershell-unable-to-connect-to-internet-at-all Thanks for your support, all. – theemperor Dec 06 '18 at 18:01

0 Answers0