Solved

string method not working

Posted on 2014-03-27
14
189 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 18

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
 

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 18

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now