+ Post New Thread
Results 1 to 5 of 5
Scripts Thread, [Powershell] IF THEN ELSE in Coding and Web Development; A client wants a very basic way of finding the owner of lost USB sticks. I have written this powershell ...
  1. #1
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    427
    Thank Post
    68
    Thanked 90 Times in 75 Posts
    Rep Power
    61

    [Powershell] IF THEN ELSE

    A client wants a very basic way of finding the owner of lost USB sticks.

    I have written this powershell script

    Code:
    #get serial from usb
    
    $serial = gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | select-string USBSTOR | foreach{$_.tostring().split("\\")[8].split("&")[0]}
    
    #cat txt file for existing serial and if exist display name
    
    $exists = cat .\somefile.txt | select-string $serial
    
    # not equal 0
    
    if ($exists -ne 0) 
    {
    echo $exists
    }
    else
    {
    $name = read-host "please enter USB owner name"
    
     echo "$serial $name" >> ./somefile.txt
    }
    which does everthing but the "Else" part.

    Any ideas?

    BoX

  2. #2

    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,168
    Thank Post
    98
    Thanked 319 Times in 261 Posts
    Blog Entries
    4
    Rep Power
    112
    Try if ($exists -ne $null) rather than 0.

  3. Thanks to jamesb from:

    box_l (23rd February 2011)

  4. #3
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    427
    Thank Post
    68
    Thanked 90 Times in 75 Posts
    Rep Power
    61


    Perfect!

    Many thanks for the speedy answer.

    BoX

  5. #4

    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,168
    Thank Post
    98
    Thanked 319 Times in 261 Posts
    Blog Entries
    4
    Rep Power
    112
    Just don't ask how many times I've been caught out by the difference between 0 and null.

  6. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,705
    Thank Post
    3,239
    Thanked 1,045 Times in 967 Posts
    Rep Power
    363
    Quote Originally Posted by jamesb View Post
    Just don't ask how many times I've been caught out by the difference between 0 and null.
    Difference is one puts you into invalid memory and the other one works

    I was going to suggest also trying or using the equivelant of Select Case or Switch ( depending on the language used ), not sure what powershell uses, I presume Switch

SHARE:
+ Post New Thread

Similar Threads

  1. Learning Powershell
    By RabbieBurns in forum How do you do....it?
    Replies: 4
    Last Post: 3rd September 2010, 12:57 PM
  2. Powershell Loop Help Please!
    By RabbieBurns in forum Scripts
    Replies: 10
    Last Post: 27th May 2010, 09:14 AM
  3. Powershell help needed please!
    By RabbieBurns in forum Scripts
    Replies: 2
    Last Post: 13th May 2010, 09:12 AM
  4. Powershell Help
    By mbedford in forum Coding
    Replies: 0
    Last Post: 1st August 2009, 03:25 PM
  5. Powershell
    By Ric_ in forum Windows
    Replies: 3
    Last Post: 25th November 2008, 11:54 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •