Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

Powershell strip string

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
Kelly Garcia
Asked:
Kelly Garcia
  • 6
  • 4
2 Solutions
 
Dan CraciunIT ConsultantCommented:
$vm=get-vm "*" + $_.split(".")[0] + "*"
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Excellent Dan!!
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Dan what does the [0] mean?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Dan CraciunIT ConsultantCommented:
The first element of an array.
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
I am trying to learn powershell :)
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Dan why doesn't this code work?

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

Open in new window

0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
Glad you got it working :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now