Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string method not working

Posted on 2014-03-27
14
Medium Priority
?
220 Views
Last Modified: 2014-03-31
Hi Guys,

I have this information stored in a variable using get-content
$s=

ADF-3.qw.ds.dss.ac.uk is down
ADF-4.qw.ds.dss.ac.uk is down
ADS-ADF-02.qw.ds.dss.ac.uk is down
ADS-ADF-03.qw.ds.dss.ac.uk is down
ADS-AGM-01.qw.ds.dss.ac.uk is down
ADS-DOM-03.qw.ds.dss.ac.uk is down
BAM-SRV-02.qw.ds.dss.ac.uk is down
DEF-DSS-02.qw.ds.dss.ac.uk is down
DEF-WEB-02.qw.ds.dss.ac.uk is down
FSR-SYN-01.qw.ds.dss.ac.uk is down
SCO-VEC-04.qw.ds.dss.ac.uk is down
MPS-CAS-01.qw.ds.dss.ac.uk is down

I only need the FQDN e.g MPS-CAS-01.qw.ds.dss.ac.uk so ive tried

 $s.Split(" ")[1]

however it throws an error message and says ;

ethod invocation failed because [System.Object[]] doesn't contain a method named 'Split'.

when I go $s | gm - there certainly is a string() method.

Please help.

Thank you in advance.
0
Comment
Question by:Kay
  • 7
  • 5
  • 2
14 Comments
 
LVL 19

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 568 total points
ID: 39958711
check  
$s.gettype()

use to convert to string
$s=$s.tostring()
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1432 total points
ID: 39958712
$S.Split(" ") wont work if you are using PowerShell version 2, as $s is an array of strings.. but will work with 3 or higher, but again it will not give you the collection of FQDN names as expected..
Result will be something like..
ADF-3.qw.ds.dss.ac.uk
is
down
ADF-4.qw.ds.dss.ac.uk
is
down
..........

Open in new window


To get all FQND's you can try something like..
$S = GC C:\test.txt | %{($_ -split " ")[0]}

$S will contain FQDN of all servers..
Result will be..
ADF-3.qw.ds.dss.ac.uk
ADF-4.qw.ds.dss.ac.uk
...

Open in new window

0
 

Author Comment

by:Kay
ID: 39958984
excellent subsun! its works!
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Kay
ID: 39958987
one of my servers has a space e.g MPS-CAS-01 .qw.ds.dss.ac.uk is down - how do I remove that space?
0
 
LVL 19

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 568 total points
ID: 39959016
To replace a whitespace in a string use:

" abdul raheman" -replace " "
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1432 total points
ID: 39959024
Simple option would be to use something like..
$S = GC C:\test.txt | %{($_ -replace " is down") -replace " "}
0
 

Author Comment

by:Kay
ID: 39959056
how do I add to a list of items in a variable.

e.g. $s=DEV-QRE-DEL-10
DEV-QRE-ESS-10
DEV-QRE-PRO-16
DEV-QRE-PRO-14
DEV-QRE-PRO-15
DEV-QRE-PRO-17
DEV-QRE-PRO-11
DEV-QRE-PRO-10
DEV-QRE-PRO-12
DEV-QRE-PRO-13

and on every single one I need to add .qw.ds.dss.ac.uk

thank you again.
0
 

Author Comment

by:Kay
ID: 39959065
I've tried  foreach ($v in $vm) {$s = $v + ".qw.ds.dss.ac.uk" }

don't work!
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1432 total points
ID: 39959068
Are you reading it from file? If yes.. Try..

$S = GC C:\test.txt | %{("$($_).qw.ds.dss.ac.uk "}

As per your last comment..
$S = $vm | %{("$($_).qw.ds.dss.ac.uk "}
0
 

Author Comment

by:Kay
ID: 39959166
thanks I've tried

$S = $vm | %{("$($_).qw.ds.dss.ac.uk "}

and the results I am getting is

@{Name=DEV-QRE-DEL-10}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-ESS-10}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-16}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-14}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-15}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-17}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-11}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-10}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-12}.qw.ds.dss.ac.uk
@{Name=DEV-QRE-PRO-13}.qw.ds.dss.ac.uk

how do we remove the @{Name= }
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1432 total points
ID: 39959170
$S = $vm | %{"$($_.name).qw.ds.dss.ac.uk "}
0
 

Author Comment

by:Kay
ID: 39959209
excellent!
0
 

Author Comment

by:Kay
ID: 39959214
how do u guys learn that stuff?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 1432 total points
ID: 39959248
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question