Solved

Powershell strip string

Posted on 2014-03-21
10
314 Views
Last Modified: 2014-03-27
Hi guys,

$server=MPS-EOP-01.qw.ds.qwe.ac.uk


now I need to put this in another variable called VM and exclude the .qw.ds.qwe.ac.uk and add * before the MPS-EOP-01 and a start after it.

how can I do this?

that's the script I am working on:

Add-PSSnapin VMware.VimAutomation.Core
$cred=get-credential
Connect-VIServer 10.2.32.456 –Credential $cred


Get-Content C:\Reboot_Servers.csv| % { if (!(Test-Connection -ComputerName $_ -Quiet))

$vm=get-vm "*" + $_ + "*"
Restart-VM -VM $VM
Start-Sleep -s 300  # this will wait for 5 minutes
   

        }

Open in new window


thank you in advance.
0
Comment
Question by:Kay
  • 6
  • 4
10 Comments
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 500 total points
ID: 39945100
$vm=get-vm "*" + $_.split(".")[0] + "*"
0
 

Author Comment

by:Kay
ID: 39945207
Excellent Dan!!
0
 

Author Comment

by:Kay
ID: 39949979
Dan what does the [0] mean?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39949980
The first element of an array.
0
 

Author Comment

by:Kay
ID: 39950974
what exactly is the first element of an array,

so if the server name is srv01.qw.as.dsl.uk

and we use the $_.split(".")[0]

does the first element mean the srv01???

whats the point of that?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39950999
split() will create an array of strings.

"srv01.qw.as.dsl.uk".split(".") will create this:
[0] srv01
[1] qw
[2] as
[3] dsl
[4] uk

You pick your element using the number. If you wanted dsl you would use
"srv01.qw.as.dsl.uk".split(".")[3]

What's the point? Answering your question, I guess :)
0
 

Author Comment

by:Kay
ID: 39952746
I am trying to learn powershell :)
0
 

Author Comment

by:Kay
ID: 39953836
Dan why doesn't this code work?

dsquery computer -name *qre* | % {$_.split('"=,)} | % {gwmi -ComputerName $_ win32_operatingsystem} | select name 

Open in new window

0
 

Author Comment

by:Kay
ID: 39953846
my apologies its working now forget the [2] and ' !
dsquery computer -name *qre* | % {$_.split('"=,')[2]} | % {gwmi -ComputerName $_ win32_operatingsystem} | select name 

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39958586
Glad you got it working :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…
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…

706 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

16 Experts available now in Live!

Get 1:1 Help Now