locked
Script Help - Working with Files - Reading/Writing RRS feed

  • Question

  • Hey Folks,

    I need help build a script that will read a text string from a specific .INI file, and then write that string to a file along with the name of the computer where the string originated from.

    I plan on deploying this script to our targeted workstations with SCCM 12, so it does not need recursive abilities. We want each of the machines which run the script to write their data to a single file with a date stamp as part of the name. The data written needs to include the unique string which follows this format "devicename=DJ########", along with the computer's name. The device name which needs to be extracted starts with DJ and is followed by an 8 digit number. It would be better if we could use export-csv than out-file. 

    This is what my very inexperienced self came up with so far:

    get-content 'C:\users\Public\Something\myfile.ini' | Select-String devicename=dj | out-file \\server\folder\data.txt -append

    I found this bit of code which should allow me to set the file name to a date, but I am unsure exactly how to implement this:

    $enddate = (Get-Date).tostring("yyyyMMdd")
    $filename = 'C:\Documents and Settings\User\Desktop\' + $enddate + '_VMReport.doc'
    $filename

    The developer of a core piece of software requires all computers have a unique ID, but provides absolutely no tools or guidance for tracking this crucial information. Any input you can provide will be greatly appreciated!

    Please help us fix this nightmare! Thank you in advance!

    Wednesday, February 25, 2015 1:48 AM

Answers

  • Rather than deploying this with SCCM, I want to be able to provide a file with lists of computers for the script to run against.

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($target in $targets) {
               $computer=hostname
               $devicename=get-content 'C:\Users\Public\myfile.ini' | Select-String devicename=
                $djnumber=$devicename -replace "devicename=","`t"
                $enddate = (Get-Date).tostring("yyyy-MM-dd")
                $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
                $prize=$computer += $djnumber
                $prize | Out-File $filename -Append

    }

    ===============

    When this runs, it only puts the info from my tech machine, not the computers in the source file.

    It looks like you should be able to make a minor change to your code like this:

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($computer in $targets) {
        $devicename=get-content "\\$computer\c$\Users\Public\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
        $enddate = (Get-Date).tostring("yyyy-MM-dd")
        $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
        $prize=$computer += $djnumber
        $prize | Out-File $filename -Append
    
    }
    

    If that works, you can make a few more minor changes to make it so that you are exporting to a CSV instead of using Out-File (your first post mentioned that you wanted to do that):

    $filename = '\\server\Library\DJs\{0}_DJNumbers.csv' -f (Get-Date -Format "yyyy-MM-dd")
    Import-csv 'C:\hold\djs.csv' | ForEach-Object {
        $computer = $_
    
        # Original code to get djnumber
        $devicename=get-content "\\$computer\c$\Users\Public\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
        
        # Create an object that will be passed to Export-Csv
        [PSCustomObject] @{
            ComputerName = $computer
            DJNumber = $djnumber
        }
    } | Export-Csv -Path $filename

    I haven't tested any of this, so don't be alarmed if it doesn't work without some tweaking. If you're running it against a bunch of machines, you'll probably want to try to add in some error handling, too.

    Wednesday, February 25, 2015 4:31 PM

All replies

  • The computer name is a unique ID.  What else do you need?

    Here is a script that reads INI files correctly: https://gallery.technet.microsoft.com/scriptcenter/Edit-old-fashioned-INI-f8fbc067

    Here are instructions on how to write a script: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 2:37 AM
  • Thank you for taking the time to reply. I have learned a TON today and I really made some big improvements. The unique IDs are created by the software developer and are not the same as the computer names... that would be far too easy!! ;)

    Here is what I came up with so far..... the best part is that it WORKS!!!!!

    $computer=hostname
    $devicename=get-content 'C:\Users\Public\Software\myfile.ini' | Select-String devicename=
    $djnumber=$devicename -replace "devicename=","`t"
    $enddate = (Get-Date).tostring("yyyy-MM-dd")
    $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
    $prize=$computer += $djnumber
    $prize | Out-File $filename -Append

    If I wanted to make this work with a source file, what would be the best way to do that?

    • Edited by Daren M Wednesday, February 25, 2015 2:58 AM Adding info
    Wednesday, February 25, 2015 2:54 AM
  • What you are asking is still fairly unclear. Try and write your script and post back with specific questions as you need help.  By trying to write the script you will be able to sort out your question a bit better I think.

    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 3:08 AM
  • Rather than deploying this with SCCM, I want to be able to provide a file with lists of computers for the script to run against.

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($target in $targets) {
               $computer=hostname
               $devicename=get-content 'C:\Users\Public\myfile.ini' | Select-String devicename=
                $djnumber=$devicename -replace "devicename=","`t"
                $enddate = (Get-Date).tostring("yyyy-MM-dd")
                $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
                $prize=$computer += $djnumber
                $prize | Out-File $filename -Append

    }

    ===============

    When this runs, it only puts the info from my tech machine, not the computers in the source file.

    • Edited by Daren M Wednesday, February 25, 2015 3:26 AM
    Wednesday, February 25, 2015 3:21 AM
  • Which may make sense to you but it doesn't make any technical sense.  We do not know what you are doing or what problems you are having.  Try asking a specific question.

    Most of the code you posted would not work and makes little sense to me.  You need something from a file.  I gave you s script that can read an ini file.  You are trying to use something else to read the ini file.

    I suspect youdo nothave an init file so posting a coapy of it or part of it would help.

    You failed to explain what you need to do with any of this other than the non-script or pseudo code  that you posted.

    It appears you have a CSV file but you are not using a CSV file in your loop.

    No - none of this makes any sense.  Start by asking a single question.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 3:29 AM
  • What do you mean the code I posted would not work?? It is working now! If you don't understand it, that's too bad.

    I just need to figure out how to make this bit of fully functional code work recursively (using foreach) on a list of computers supplied in a source file:

    $computer=hostname
    $devicename=get-content 'C:\Users\Public\Software\myfile.ini' | Select-String devicename=
    $djnumber=$devicename -replace "devicename=","`t"
    $enddate = (Get-Date).tostring("yyyy-MM-dd")
    $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
    $prize=$computer += $djnumber
    $prize | Out-File $filename -Append

    Something like this:

    $targets=Import-csv 'C:\hold\djs.csv'
     $targets | foreach {

    Or this:

    $targets=Import-csv 'C:\hold\djs.csv'
     foreach($target in $targets){

    Wednesday, February 25, 2015 3:40 AM
  • This line is impossible:

    $computer=hostname

    It cannot possibly work.

    There is nowhere in the code that you are using $target.

    Look over this code very carefully and try to understand why it cannot work.

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($target in $targets){
    	$computer=hostname #<=== WON'T WORK
     	$devicename=get-content 'C:\Users\Public\myfile.ini' | Select-String devicename=
    	$djnumber=$devicename -replace "devicename=","`t"
    	$enddate = (Get-Date).tostring("yyyy-MM-dd")
    	$filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
    	$prize=$computer += $djnumber
    	$prize | Out-File $filename -Append
    }
    
    #the CSV file is never used and the rest of the code makes little sense.
    Please try to ask a specific question. It is the only way we will be able to sort out what you are asking.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 3:56 AM
  • I explained what I am doing in the original post and every subsequent post. What I don't understand is your aggression.

    Please read this and everything else I posted, and you'll know exactly what I am doing and why:

    "I need help build a script that will read a text string from a specific .INI file, and then write that string to a file along with the name of the computer where the string originated from.

    I plan on deploying this script to our targeted workstations with SCCM 12, so it does not need recursive abilities. We want each of the machines which run the script to write their data to a single file with a date stamp as part of the name. The data written needs to include the unique string which follows this format "devicename=DJ########", along with the computer's name. The device name which needs to be extracted starts with DJ and is followed by an 8 digit number. It would be better if we could use export-csv than out-file." 

    Thanks for the help?

    Wednesday, February 25, 2015 3:58 AM
  • What is in $targets?
    What is in the INI file?

    How are you trying to connect all of this to multiple machines?

    Again.  If you ask a single question such as "How do I read an INI file?" or "How do I connect to a computer?

    And what is this line supposed to be:

    $computer=hostname #<=== WON'T WORK


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 4:04 AM
  • As an example, here is how to read the ini file values.

    $sb=New-Object System.Text.StringBuilder(256)
    [profileapi]::GetPrivateProfileString('Computers','devicename','',$sb,$sb.Capacity,"$pwd\computers.ini")
    $devicename=$sb.ToString()

    Here is how to read a CSV file.

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($target in $targets){
        $targetname=$target.Name
    }

    You have to reference items in the CSV as fields on an object.  A CSV import is an object collection.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 4:15 AM
  • I suspect it would help to show you what an INI file looks like.

    ==== INI FILE ===
    [section one]
    name1=value1
    name2=value2

    [section two]All nameswith
    name1=value1
    name2=value2
    ....

    ; comments
    ; start with a semicolon

    === END OF INI ====

    All names within a section must be unique.

    A file without a [section...] is not an ini file  It may de a string data file which can be loaded with "ConvertFrom-StringData"


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 4:28 AM
  • Let's break it down, in the beginning I was planning on deploying a script to our 600 workstations using SCCM 2012 R2. The context was different at that time, and $computer=hostname absolutely 100% WOULD WORK for that script context because it would be running locally on every computer, driven by SCCM. You are correct that it will not work in the new context: a script run form a single workstation to poll a list of domain machines.

    My method for extracting the text string works well enough. It delivers exactly the information I need, yeah it could be more concise, but if it's working..... I don't need to parse the INI file or anything fancy, we're just reading it like a text doc and selecting some text. Also, I know what they look like, what they do, etc. No need for the lesson there, thanks any way!

    The current error I am getting is this:

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At line:7 char:8

    + $prize=$computer += $djnumber

    +        ~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    This seems very odd because it worked previously.

    You keep telling me to ask specific questions, and I am. I want to know how to make my script that works on a single machine work on a list of computers supplied in a csv or txt or doc..... file.

    Imagine this: A technician creates file with a list of domain computers, names it correctly and places it in the directory specified in the script. Technician then runs the script, the script polls all computers on the list and then appends the ID# and the computer's name into a single file on a network share.

    What is not clear?



    • Edited by Daren M Wednesday, February 25, 2015 4:53 AM
    Wednesday, February 25, 2015 4:51 AM
  • $prize=$computer += $djnumber

    is illegal because of something you have created in the part of your script that you are not showing.  Without being able to get answers to the questions I asked I cannot be of much help.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 4:57 AM
  • Imagine this: A technician creates file with a list of domain computers, names it correctly and places it in the directory specified in the script. Technician then runs the script, the script polls all computers on the list and then appends the ID# and the computer's name into a single file on a network share.


    That is more like a question.  

    To use a list of computers I suspect you may mean a file list

    Get-Content computers.txt |
        ForEach-Object{
           Write-Host "Here is a computer from the list --> $_" -fore green
       }

    That is how we would read a list.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 5:00 AM
  • Yes this will work:

    $computer=hostname

    I forgot about the CMD shell program called "hostname".  Normally in PowerShell we would do this: "$computer=$env:COMPUTERNAME" which is the PowerShell equivalent.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 5:06 AM
  • Your MyFile.INI is likely the cause of your error as it likely contains multiple lines that match the select.

    Again - without some indication of what is in the ini files I cannot even give a good guess as to what you are doing.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 5:10 AM
  • You are not helpful at all. You clearly do not have much experience with powershell and just came here to belittle me. Please don't waste anymore of my time with your condescending posts. You seem to  deliberately ignore key details which I repeatedly explain from numerous angles .

    You have contributed nothing in all of you many posts. Please don't reply any more,you're just pissing me off.


    • Edited by Daren M Wednesday, February 25, 2015 2:48 PM
    Wednesday, February 25, 2015 2:48 PM
  • Sorry but I have been using PowerShell since the earliest beta and have been a Windows Developer for almost 30 years.

    You are not asking your question very well and have not bothered to tell us the contents of your files.  You are demanding answers to vague questions and clearly do not have any experience with PowerShell.

    No one is belittling you. You are have issues because you lack fundamental knowledge about things like file structures and how PowerShell manages variables.  We can try and help you but you are going to have to concentrate on answering these questions. No one is going to be able to guess at these answers.

    I will leave you with a recommendation that you spend some time learning how to use PowerShell before pursuing this any farther.

    Here is a good place to start: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

    Good luck. I am sure you will eventually sort this out.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 2:54 PM
  • Then your problem is reading and understanding the English language, I repeatedly explained exactly what I was doing in detail and you just ignored everything I wrote. You constantly tell me to ask basic questions, which were below the level of the discussion and the level of this script. You INSISTED that my code was not even code. IN ALL CAPS RAGE you say stuff like "WILL NOT WORK", then you see that it will because hostname is valid in the PS context. You are an arrogant person who has very little to contribute. When you found issues with my code, you tell me to find them myself!!! What kind of help is that??? If you see something or understand something better than someone else, you should EXPLAIN it. Don't just tell them to look more closely, that's not how help forums work for crying out loud!! If I knew how to fix my issue, I wouldn't post anything here!!!! Don't you get that fundamental concept???

    Now you suggest that I RTFM, right? Because that's so helpful! YOU SUCK AT THIS.

    If you cannot understand something, you don't need to reply. If you understand something better than another person, don't belittle them. You have done NOTHING to help me, now please go away. 

    I came here to get help with a script and you have provided ZERO assistance. I have already flagged your replies as off topic, and I would block you altogether if I could.




    • Edited by Daren M Wednesday, February 25, 2015 3:40 PM
    Wednesday, February 25, 2015 3:34 PM
  • I asked you repeatedly to post a sample of your files. You completely ignored that.  I asked you to post all of the script - you ignored that.  I asked why you were using a CSV in a loop and yet never using it.  I showed you how tot read a file list of computer names and you ignored that.

    I have to assume that your whole purpose is to argue every point and provide no useful information about what it is you are doing. 

    I know you would like someone to just hand you a complete solution.  That cannot happen because the information you have provided cannot be used to figure out what you are asking.

    Usually this much trouble is a result of a user who has a very limited understanding of scripting and the technology being discussed.  I cannot compensate for what you do not understand.  That is why I recommended that you take some time to use the learning materials.  If you think you already know all of that then I guess you will have to wait for someone who can decode what it is you are asking.  Maybe it is just me but I cannot make any sense at all of what you are asking.  You say your code works and yet you want help and cannot put your request in the form of a simple question.  The "sort of" questions that I answered you either argued with or ignored.  I have to give up.  I am sorry that we cannot communicate.  Maybe someone else can sort you out.


    ¯\_(ツ)_/¯

    Wednesday, February 25, 2015 3:58 PM
  • Hi Daren M, this is not intended to be rude, but rather instructive. I agree with jrv that you need to provide a razor-sharp focus on one very specific question and ask for help with that.

    Usually, this means writing a very short script that contains only the absolute minimum amount of code needed to reproduce the problem. You must also be very specific about what the problem is (what you are expecting to happen, and what happened instead). Also, please copy-and-paste any error messages. Remember: We can't read your mind, nor can we see your screen. We only know what you post in the forum.

    Since your script is parsing some other file, you need to give an example of what that file looks like and what specifically you want to do with it.

    The basic principle here is that you are asking for free help in a public forum. Respondents are busy people, many with full-time jobs. Make it as simple as possible for others to help you.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 25, 2015 4:22 PM
    Moderator
  • Rather than deploying this with SCCM, I want to be able to provide a file with lists of computers for the script to run against.

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($target in $targets) {
               $computer=hostname
               $devicename=get-content 'C:\Users\Public\myfile.ini' | Select-String devicename=
                $djnumber=$devicename -replace "devicename=","`t"
                $enddate = (Get-Date).tostring("yyyy-MM-dd")
                $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
                $prize=$computer += $djnumber
                $prize | Out-File $filename -Append

    }

    ===============

    When this runs, it only puts the info from my tech machine, not the computers in the source file.

    It looks like you should be able to make a minor change to your code like this:

    $targets=Import-csv 'C:\hold\djs.csv'
    foreach($computer in $targets) {
        $devicename=get-content "\\$computer\c$\Users\Public\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
        $enddate = (Get-Date).tostring("yyyy-MM-dd")
        $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
        $prize=$computer += $djnumber
        $prize | Out-File $filename -Append
    
    }
    

    If that works, you can make a few more minor changes to make it so that you are exporting to a CSV instead of using Out-File (your first post mentioned that you wanted to do that):

    $filename = '\\server\Library\DJs\{0}_DJNumbers.csv' -f (Get-Date -Format "yyyy-MM-dd")
    Import-csv 'C:\hold\djs.csv' | ForEach-Object {
        $computer = $_
    
        # Original code to get djnumber
        $devicename=get-content "\\$computer\c$\Users\Public\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
        
        # Create an object that will be passed to Export-Csv
        [PSCustomObject] @{
            ComputerName = $computer
            DJNumber = $djnumber
        }
    } | Export-Csv -Path $filename

    I haven't tested any of this, so don't be alarmed if it doesn't work without some tweaking. If you're running it against a bunch of machines, you'll probably want to try to add in some error handling, too.

    Wednesday, February 25, 2015 4:31 PM
  • Hi Bill, thank you very much for replying to my post. I have already fixed everything and have a working script. I thought all of the posts I have are pretty detailed, but I am happy to clarify it.


    All domain computers have a file (myfile.ini) which contains a unique string like this:

    devicename=DJ12345678

    For each domain computer, I want to grab this string and write it along with the computer's name with which it is associated to a file on a network share.


    • Edited by Daren M Wednesday, February 25, 2015 7:14 PM
    Wednesday, February 25, 2015 7:03 PM
  • Hi Daren M, that's a good example of a good question. Very short and very specific.

    Rohn Edwards' post contains a good example of how you could write code to do this.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 25, 2015 7:05 PM
    Moderator
  • Hi Rohn,

    This is very helpful, thank you so much for taking the time to reply! Your code looks fantastic and has already given me some ideas. Here is the working script that I have come up with:

    Get-Content 'C:\DJs\complist.csv' |
     ForEach-Object {
        $devicename=get-content "\\$_\C$\Users\Public\Software\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
        $enddate = (Get-Date).tostring("yyyy-MM-dd")
        $filename = '\\server\Library\DJs\' + $enddate + '_DJNumbers.csv'
        $prize=$_ += $djnumber
        $prize | Out-File $filename -Append
    
        }

    I will play around the PSCustomObject and some of the other ideas you posted! THANK YOU!!!

    My Systems Admin requested that instead of creating a file with a date stamp, she wants me to make a file with a standard name (like djnumbers.csv). This script will run on a scheduled task, so when it runs it should rename any existing file to yesterday's date (ie 2015-02-24_djnumbers.csv). This way we will have a historical record of all changes to DJ#'s for all machines and we will appease an Access DB.

    How do I check for an existing file, and if it exists, rename it to yesterday's date?

    Wednesday, February 25, 2015 7:13 PM
  • How do I check for an existing file, and if it exists, rename it to yesterday's date?

    You can use Test-Path to check and Rename-Item to make the change:

    http://ss64.com/ps/test-path.html

    http://ss64.com/ps/rename-item.html


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Wednesday, February 25, 2015 7:18 PM
  • Hi Bill, I'm glad you think so. :)

    This read pretty clearly to me, but people are different:

    "I need help build a script that will read a text string from a specific .INI file, and then write that string to a file along with the name of the computer where the string originated from.

    I plan on deploying this script to our targeted workstations with SCCM 12, so it does not need recursive abilities. We want each of the machines which run the script to write their data to a single file with a date stamp as part of the name. The data written needs to include the unique string which follows this format "devicename=DJ########", along with the computer's name. The device name which needs to be extracted starts with DJ and is followed by an 8 digit number. It would be better if we could use export-csv than out-file.

    Wednesday, February 25, 2015 7:19 PM
  • The shorter the better. There is an acronym for this: TL;DR (too long; didn't read).


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 25, 2015 7:21 PM
    Moderator
  • I don't see the value of discussing the issue of English syntax here. User jvc did not understand my code, he repeatedly told me functional commands wouldn't work and suggested I go RTFM. User Rohn Edwards clearly understood exactly what I was attempting to accomplish and provided some great ideas. If you read through each post, I reiterate everything numerous times.

    TL;DR jvc didn't understand, Rohn obviously did, this proves my description was not all that bad.

    Wednesday, February 25, 2015 7:28 PM
  • How do I check for an existing file, and if it exists, rename it to yesterday's date?

    You can use Test-Path to check and Rename-Item to make the change:

    http://ss64.com/ps/test-path.html

    http://ss64.com/ps/rename-item.html




    This is great, thanks for replying!

    How would you use rename-item to name something with yesterday's date?


    • Edited by Daren M Wednesday, February 25, 2015 7:32 PM
    Wednesday, February 25, 2015 7:32 PM
  • This is great, thanks for replying!

    How would you use rename-item to name something with yesterday's date?

    You're welcome.

    Here's an example:

    $date = (Get-Date).AddDays(-1)
    
    Rename-Item -Path .\file.txt -NewName ("file-{0}.txt" -f (Get-Date $date -Format MM-dd-yyyy))


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Wednesday, February 25, 2015 7:52 PM
  • If you feel good about your question, great. IMO concise is better than verbose. The principle is to make it as easy as possible for others to help you. The less they have to read, the better.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 25, 2015 7:59 PM
    Moderator
  • I am trying to add a step where the script grabs a list of all domain workstations and saves it to a file. This file is then used as the input for the rest of the script. There is a problem with the way the computers names are imported using this method. Here is the script:


    # Rename the existing DJNumbers.csv to yesterday's date
    $date = (Get-Date).AddDays(-1)
    Rename-Item -Path '\\Server\Library\DJs\DJnumbers.csv' -NewName ("DJNumbers_{0}.csv" -f (Get-Date $date -Format yyyy-MM-dd))
    
    #Save a list of all current AD computers
    Get-ADComputer -Filter * | select name | Out-File \\Server\C$\DJs\complist.csv
    
    #Extract the DJ number for every computer in complist.csv
    Get-Content '\\Server\C$\DJs\complist.csv' |
     ForEach-Object {
        $devicename=get-content "\\$_\C$\Users\Public\myfile.ini" | Select-String devicename=
        $djnumber=$devicename -replace "devicename=","`t"
    
        $prize=$_ += $djnumber
        $prize | Out-File \\Server\Library\DJs\DJnumbers.csv -Append
    
    }
    
    

    The problem is that the computer names include a ton of blank spaces if I use Out-File in the line:

    #Save a list of all current AD computers
    Get-ADComputer -Filter * | select name | Out-File \\Server\C$\DJs\complist.csv

    Error looks like this:

    get-content : Cannot find path '\\CM112233                                                                                                      
                                      \C$\Users\Public\Software\myfile.ini' because it does
    not exist.
    At line:11 char:13
    + $devicename=get-content "\\$_\C$\Users\Public\Software\...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (\\CM112233       ...ction\myfile.ini:String) [Get-Content], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


    If I use Export-Csv, it puts ""'s around the computer names and that breaks it as well.

    #Save a list of all current AD computers
    Get-ADComputer -Filter * | select name | Export-Csv \\Server\C$\DJs\complist.csv

    This is the error:

    get-content : Illegal characters in path.
    At line:11 char:13
    + $devicename=get-content "\\$_\C$\Users\Public\Software\  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (\\"CM112233"\C$\U...are\myfile.ini:String) [Get-Content], ArgumentException
        + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetContentCommand



    • Edited by Daren M Wednesday, February 25, 2015 11:15 PM
    Wednesday, February 25, 2015 11:14 PM
  • Use


    select -expandproperty Name
    

    instead of just


    select Name
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 25, 2015 11:36 PM
    Moderator
  • Use


    select -expandproperty Name
    

    instead of just


    select Name
    


    -- Bill Stewart [Bill_Stewart]

    Thank you!!! That worked perfectly! Can you please explain what is happening here?

    Thursday, February 26, 2015 12:37 AM

  • PS C:\> help select-object -full
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 26, 2015 2:50 AM
    Moderator

  • PS C:\> help select-object -full
    


    -- Bill Stewart [Bill_Stewart]

    Select-Object

        -------------------------- EXAMPLE 2 --------------------------   

    PS C:\>Get-Process Explorer | Select-Object –Property ProcessName -ExpandProperty Modules | Format-List 

    ProcessName       : 00THotkey   

    Size              : 256   

    Company           : TOSHIBA Corporation   

    FileVersion       : 1, 0, 0, 27   

    ProductVersion    : 6, 2, 0, 0   

    Description       : THotkey   

    Product           : TOSHIBA THotkey   

    ModuleName        : 00THotkey.exe   

    FileName          : C:\WINDOWS\system32\00THotkey.exe   

    BaseAddress       : 4194304       

    This command gets information about the modules used by the processes on the computer. It uses Get-Process cmdlet to get the process on the computer. It uses the Select-Object cmdlet to create new objects with only the selected properties. The command uses the Property parameter of the Select-Object cmdlet to select the process names. Because the Modules property contains an ModuleProcess object that has many properties, the command uses the ExpandProperty parameter to get the properties of the objects in the Modules property of each process. The command uses the Format-List parameter to display the name and modules in of each process in a list.

    Do you think you could explain this in plain English? Thanks for the help!

    Thursday, February 26, 2015 3:26 AM
  • There is no need to be helpless. You can find information too, if you try.

    http://www.bing.com/search?q=powershell+select-object


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 26, 2015 1:07 PM
    Moderator
  • Why are you a forum mod if you just tell people to RTFM?

    Why would you say someone is helpless for asking for a plain English description? Geez, what kind of forum is this?


    If you don't see the value of person-to-person communication, why have a forum at all?
    • Edited by Daren M Thursday, February 26, 2015 3:02 PM
    Thursday, February 26, 2015 2:59 PM
  • Why are you a forum mod if you just tell people to RTFM?

    Why would you say someone is helpless for asking for a plain English description? Geez, what kind of forum is this?


    If you don't see the value of person-to-person communication, why have a forum at all?

    The purpose of the forum is to provide guidance for people trying to learn Powershell. 

    In just the base installation of Powershell V4, before adding any modules, I have 2356 cmdlets that have an collective total of 4366 parameters.  

    Learning powershell by having someone explain every usage of every parameter of every cmdlet  is not a viable solution.

    You have to learn to use the available help texts if you're going to learn to use Powershell effectively in any reasonable amount of time.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 3:19 PM
    Moderator
  • 1. There's no shortcut to rolling up your sleeves and learning how to try things out on your own and search for solutions.

    2. It's not the purpose of this forum to try and train everyone on every possible detail. Please keep in mind that the people answering questions in this forum do so out of good will and as volunteers; there is no service-level agreement that requires anyone to give answers just because someone demands them. (I would also point out that demanding answers is a pretty ineffective way of getting free help.)


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 26, 2015 3:50 PM
    Moderator
  • 1. There's no shortcut to rolling up your sleeves and learning how to try things out on your own and search for solutions.

    2. It's not the purpose of this forum to try and train everyone on every possible detail. Please keep in mind that the people answering questions in this forum do so out of good will and as volunteers; there is no service-level agreement that requires anyone to give answers just because someone demands them. (I would also point out that demanding answers is a pretty ineffective way of getting free help.)


    -- Bill Stewart [Bill_Stewart]

    When did I demand anything? What makes you think I am averted to learning? Isn't it clear from my numerous posts in this thread that I did a lot of research and study to get where I am today?

    The problem with telling people to go RTFM in the way you did is that you assume that I didn't already read about this thing. The fact is I did read the help file, but it was still unclear.

    I never made any statement indicating a sense of entitlement or laziness. I asked for a human-to-human description and you called me helpless. That is completely uncalled for and totally unprofessional on your part. I have shown sincere gratitude to each individual who has provided helpful information.

    I am not sure who these general comments are directed at, but it clearly is not relevant to this case.


    • Edited by Daren M Thursday, February 26, 2015 4:38 PM
    Thursday, February 26, 2015 4:33 PM
  • Why are you a forum mod if you just tell people to RTFM?

    Why would you say someone is helpless for asking for a plain English description? Geez, what kind of forum is this?


    If you don't see the value of person-to-person communication, why have a forum at all?

    The purpose of the forum is to provide guidance for people trying to learn Powershell. 

    In just the base installation of Powershell V4, before adding any modules, I have 2356 cmdlets that have an collective total of 4366 parameters.  

    Learning powershell by having someone explain every usage of every parameter of every cmdlet  is not a viable solution.

    You have to learn to use the available help texts if you're going to learn to use Powershell effectively in any reasonable amount of time.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    I didn't ask for a description of 4366 parameters, I asked for clarity on ONE after I had already read the help file.

    Thursday, February 26, 2015 4:35 PM
  • Daren - can you see that now, after getting push-back from nearly everyone, that maybe the problem is in something you are doing.

    As Bill has tried to patiently coax you into seeing, the forum is not here to incrementally teach you how to use PowerShell.  You need to learn the basics; specifically how to use help for a starter.  You are also asking multiple chained questions which is outside of the scope and purpose of this forum.  YOur original question has been answered multiple times.  It is up to you to learn enough to understand how to apply that information.  You can start a knew topic with other questions as they come up.  In fact starting fresh with your next issue would help you to get an answer fairly quickly.

    Learn to focus on a specific problem.  Don't just throw everything out in a big heap and act helpless.  Try suggestions and ask why someone suggest a particular solution or asks a particular question.  Collaborate.  Don't debate.


    ¯\_(ツ)_/¯

    Thursday, February 26, 2015 4:43 PM
  • Daren - can you see that now, after getting push-back from nearly everyone, that maybe the problem is in something you are doing.

    As Bill has tried to patiently coax you into seeing, the forum is not here to incrementally teach you how to use PowerShell.  You need to learn the basics; specifically how to use help for a starter.  You are also asking multiple chained questions which is outside of the scope and purpose of this forum.  YOur original question has been answered multiple times.  It is up to you to learn enough to understand how to apply that information.  You can start a knew topic with other questions as they come up.  In fact starting fresh with your next issue would help you to get an answer fairly quickly.

    Learn to focus on a specific problem.  Don't just throw everything out in a big heap and act helpless.  Try suggestions and ask why someone suggest a particular solution or asks a particular question.  Collaborate.  Don't debate.


    ¯\_(ツ)_/¯

    Kick sand.

    When I asked about the function of a specific parameter, I am told to go read about it myself. Now you tell me I need to be specific. JVC, please don't reply to my posts, you contribute nothing.

    • Edited by Daren M Thursday, February 26, 2015 4:54 PM
    Thursday, February 26, 2015 4:50 PM
  • I didn't ask for a description of 4366 parameters, I asked for clarity on ONE after I had already read the help file

    There is more to using the help than just reading the description.  Look at the samples provided.  Copy and paste them into your ISE and try the out.  See what the results are and correlate that to the description.  

    The help files aren't always intuitive to new users at first.  Phrases and terminology may be unfamiliar and need to be correlated to results to become intuitive when they're encountered later.  You'll find the same thing when you start needing to decipher error messages.  It's a learning process.  It requires some effort, but it's not all that difficult and doing it early will help get you up to speed quicker.  We're telling you that you need to do this because we've all been there, and done it.


     

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, February 26, 2015 4:58 PM
    Moderator
  • No Daren.  We are trying to point you in a more functional and less frustrating (for you)  direction. You only interpret things as if we are conspiring against you.  I know this is complex and frustrating at first.  Learning the fundamentals will clear this up and make it all seem clear. Believe me.  Allof us go back to the documentation frequently. 

    Yesterday I sat in on a course at MVA by Jeffery Snover, the architect of PowerShell, Even Jeff noted that he has to frequently use HELP and he built the damn thing.


    ¯\_(ツ)_/¯

    Thursday, February 26, 2015 4:59 PM
  • No Daren.  We are trying to point you in a more functional and less frustrating (for you)  direction. You only interpret things as if we are conspiring against you.  I know this is complex and frustrating at first.  Learning the fundamentals will clear this up and make it all seem clear. Believe me.  Allof us go back to the documentation frequently. 

    Yesterday I sat in on a course at MVA by Jeffery Snover, the architect of PowerShell, Even Jeff noted that he has to frequently use HELP and he built the damn thing.


    ¯\_(ツ)_/¯

    YES, jvc, KICK SAND AND LEAVE ME ALONE. YOU ARE NOT HELPFUL, YOU ARE ONLY ARROGANT. I READ THE HELP FILES AND STILL DIDN'T UNDERSTAND. LEAVE ME ALONE.

    Thursday, February 26, 2015 5:03 PM
  • Maybe this will help: https://www.youtube.com/watch?v=kQFKtI6gn9Y

    ¯\_(ツ)_/¯

    Thursday, February 26, 2015 5:06 PM
  • Maybe this will help: https://www.youtube.com/watch?v=kQFKtI6gn9Y

    ¯\_(ツ)_/¯

    What is your problem, why do you insist on replying here? Why can't you just leave me alone? I never asked for your help. I don't like you because you are arrogant. I'm not arguing with you, I am telling you leave me alone. Why is that so hard for you to understand?

    Thursday, February 26, 2015 5:14 PM