Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell strip string

Posted on 2014-03-21
10
Medium Priority
?
361 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
[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
  • 6
  • 4
10 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 2000 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
LVL 35

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
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 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 35

Expert Comment

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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

636 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