Solved

string method not working

Posted on 2014-03-27
14
210 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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