Solved

string method not working

Posted on 2014-03-27
14
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 19

Assisted Solution

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

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

Assisted Solution

by:Subsun
Subsun earned 358 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 142 total points
ID: 39959016
To replace a whitespace in a string use:

" abdul raheman" -replace " "
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 358 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 358 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 358 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 358 total points
ID: 39959248
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

632 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