Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string method not working

Posted on 2014-03-27
14
Medium Priority
?
216 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 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
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.

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

722 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