Newbie+ questions about PowerShell

Welcome Forums General PowerShell Q&A Newbie+ questions about PowerShell

This topic contains 13 replies, has 4 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #103661

    Participant
    Points: 0
    Rank: Member

    Greetings.

    I've started to study PowerShell a short time ago and already met a wall of lacking of experience. I need some help.
    Well, what I exactly can't do:

    I have two text files:
    C:\Testing\0zero.txt – which is absolutely blank
    C:\Testing\Adresses.txt – which contains next three strings:
    11-0002-1
    11-0003-1
    11-0004-1

    What do I need #1:
    Copy 0zero.txt file to the \\"first string of Adresses.txt"\c$\Testing\

    What am I doing #1:

    Copy-Item C:\Testing\0zero.txt \\$a.Get(1)\c$\Testing\

    But it isn't work. However

    Copy-Item C:\Testing\0zero.txt \\11-0002-1\c$\Testing\

    works well.

    What do I need #2:
    Copy 0zero.txt file to the adresses which contains all strings from Adresses.txt

    What am I about to do #2 (as #1 is not work I can't progress further):

    for ($i=0; $i -le $a.Count; $i++) {Copy-Item C:\Testing\0zero.txt \\$a.Get($i)\c$\Testing\}

    Please, tell me what am I doing wrong and how to do it correctly?
    And if you have some sources of training (or tasks as above) for "Newbie+"-level please do share it with me.
    Thank you in advance.

  • #103663

    Participant
    Points: 9
    Rank: Member

    If I got you right you need something like this:

    $DestinationList = Get-Content -Path 'C:\Testing\Adresses.txt'
    Foreach($Destination in $DestinationList){
        Copy-Item -Path 'C:\Testing\0zero.txt' -Destination "\\$($Destination)\c$\Testing"
    }

    If you like to learn the very basics of Powershell you could watch the free video course at the Microsoft Virtual Academy – Getting Started with Powershell. It's pretty entertaining as well.

    • #103664

      Participant
      Points: 0
      Rank: Member

      It worked, thanks!

      But how do $Destination understands it parameters? We didn't declare any data for it. So why it is stands for exactly one line, not a symbol, or all text, for example?

      About video course, I did watched it, and "Scripting Guy" series "Learn it before it is an emergency", but it is provide basic information which I do have for now. I know how to get the list of running services and can stop them, and etc. But I need something more.
      So, then I've tried to search some tasks, which is called as "projects" and it is like "write a script for a chess on PowerShell". Which obviously I can't do.

      I need something between "check status of services" and "write a script for a chess". And I'm stuck here for a lot of time.

    • #103721

      Participant
      Points: 19
      Rank: Member

      So in general PS will only unravel collections with loops like that.

      foreach ($item in $collection) { }

      So although strings are, in some sense, a collection of characters, in PS they are a singleton; a single object.

      If you wanted to make it enumerate the characters in a string, you are still able to do that explicitly by casting the string to a character array:

      foreach ($char in [char[]]$string) { }

      But in general PS will only unravel things that are explicitly defined as collections — arrays, arraylists, and generic lists.

      If you have any background in C#, you can think of it in more explicit terms. Collections that PS can automatically enumerate without typecasting are those defined as enumerable, which in .NET contexts (which PowerShell operates under) implement the IEnumerable interface.

  • #103666

    Participant
    Points: 9
    Rank: Member

    Hmmm ... for my understanding, what you just asked for is basic stuff ... 😉 anyway ...

    We didn't declare any data for it. So why it is stands for exactly one line, not a symbol, or all text, for example?

    Yes we did. With

    Foreach($Destination in $DestinationList){

    we told Powershell to do the same step for every single item in the array named $DestinationList. And the variable it should use for this is named $Destination.

    This may lead you to the next stage: MVA Advanced Tools & Scripting with PowerShell 3.0 Jump Start.

    • #103669

      Participant
      Points: 0
      Rank: Member

      Well, it's time to some practice then. Thanks a lot.

  • #103858

    Participant
    Points: 0
    Rank: Member

    Well, it seems that I met a problem that I can't solve by myself again. I've tried by different ways but none of them was expecting results.

    What do I need:
    Make an array, which contains elements of other array.
    For Example, I have an array called Adresses, $Adresses:
    11-0001-2
    11-0002-2
    11-0003-2
    11-0004-2
    And I want to make an array $Region which should contain first two symbols of each element of $Adresses, in this case:
    11
    11
    11
    11

    What am I trying to do:

    for ($i=0; $i -lt $Adresses.Count; $i++) { 
        $Region += $Adresses[$i][0..1] 
    }

    But it returns a separate numbers, just:
    1
    1
    1
    1
    1
    1
    1
    1

    It looks like Joel Sallow talked about it in some post above, but I didn't get it without an example.

    • #103864

      Participant
      Points: 9
      Rank: Member

      You might start a new threads when you have a new question next time. And it would be good when the subject you use for this thread already leads a little bit to your actual issue. Subjects like "questions about PowerShell" do not help in a Powershell forum. 😉

      Just because you showed this again. In Powershell when you have a given collection of items (an array) you don't need to create a loop from first element to last element. You simple iterate over all containing elements with

      foreach($SingleElement in $ListOfElements){
          do something with the $SingleElement
      }

      Powershell does a lot of cool stuff implicitly for you you would have to do explicitly in other programming languages. You should try to use this.

    • #103964

      Participant
      Points: 0
      Rank: Member

      Oh, I noticed this post only now.

      Well, a lot of forums do not like thread-starting, so I didn't create a new topic.
      I will do next time.
      And I will try to concretize my issues at name of topic next time.

      I got it.

  • #103859

    Participant
    Points: 0
    Rank: Member

    something like this may work for you

    ForEach($Address in $Addresses)
    {
    $Region = $Address.split("-")[0]
    }

    • #103861

      Participant
      Points: 0
      Rank: Member

      It is not work, sadly.
      It takes only one value from array, not all of them.

      So it went like this
      Adresses >> Region
      11-0001-2 >> 11
      11-0002-2
      11-0003-2
      11-0004-2

      And I need to make it

      Adresses >> Region
      11-0001-2 >> 11
      11-0002-2 >> 11
      11-0003-2 >> 11
      11-0004-2 >> 11

    • #103865

      Participant
      Points: 9
      Rank: Member

      You should show your actual code. 😉

      $AddressList = @(
      '11-0001-2',
      '11-0002-2',
      '11-0003-2',
      '11-0004-2'
      )
      
      $NewAddressList = foreach($Address in $AddressList){
          [PSCustomObject]@{
              Address = $Address
              Region = $Address.split("-")[0]
          }
      }
      $NewAddressList

      Result:

      Address   Region
      -------   ------
      11-0001-2 11    
      11-0002-2 11    
      11-0003-2 11    
      11-0004-2 11
    • #103963

      Participant
      Points: 0
      Rank: Member

      I attached it. It was just 3 lines.
      Without it I can't continue further, so I simply do not have it.

      Well, I'll start from easiest for me, what should be a result of script-executing.
      I have some outer file with adresses of other PC, C:\Scripting\CertificateRequesting\Workstations.txt, for example
      11-0001-1
      12-0001-1
      13-0001-1
      16-0001-1

      Script should take all this values as variable and do next things with it:

      It should copy certain folder and name it as outer adress.

      If first two symbols is 11, 34, 40 and some other values, it should copy C:\Scripting\CertificateRequesting\_Templates\CertificateRequest1

      If first two symbols is 12, 52, 76 and some other, it should copy C:\Scripting\CertificateRequesting\_Templates\CertificateRequest2

      And so on.

      So result of 1st procedure should be:

      Copy-Item C:\Scripting\CertificateRequesting\_Templates\CertificateRequest1 C:\WorkFolder\11-0001-1
      Copy-Item C:\Scripting\CertificateRequesting\_Templates\CertificateRequest2 C:\WorkFolder\12-0001-1
      Copy-Item C:\Scripting\CertificateRequesting\_Templates\CertificateRequest3 C:\WorkFolder\13-0001-1
      Copy-Item C:\Scripting\CertificateRequesting\_Templates\CertificateRequest4 C:\WorkFolder\16-0001-1

      But to do it I need an individual array which contains only first two symbols of each value from outer file.

      Your attached code is displaying array "Region", but I can't use it as array, can't get values from it to code next things.

      So first I'll do:

      $OuterAdresses = Get-Content "C:\Scripting\CertificateRequesting\Workstations.txt"

      So I'll have:

      $OuterAdresses[0] - 11-0001-1
      $OuterAdresses[1] - 12-0001-1
      $OuterAdresses[2] - 13-0001-1
      $OuterAdresses[3] - 16-0001-1

      And I need to do $Region, which will have

      $Region[0] - 11
      $Region[1] - 12
      $Region[2] - 13
      $Region[3] - 16

      To use it for selecting folders for copying as on example above.

  • #103970

    Participant
    Points: 9
    Rank: Member

    Sorry if that hurts or if you think that sounds rude or unpolite but that's really not my intention. You should urgently urgently start learning the very basics of Powershell from scratch. We cannot teach you how to use the language elemtents of a scripting language in a forum. And we cannot write your scripts for you. What you're asking for is usualy covert by the very first lessons of any book or tutorial or course. If you don't want to or cannot spend money for it you could watch the free video course at the Microsoft Virtual Academy – Getting Started with Powershell. It's even slightly entertaining and will equip you with everything you need for what you're asking for in this thread.

The topic ‘Newbie+ questions about PowerShell’ is closed to new replies.