converting string to number in powershell - powershell

I am trying to convert a string to a 2 decimal place value
I have a script that has a line of code which is
(get-mailboxdatabase xxx -status).databasesize
This returns the size something like
1.008 GB (1,082,195,968 bytes)
I want to be able to convert this to a number (1.008) and can't work out how to do it.
I know about ToGB() but that only works when running the script from EMS.
I need to be able to run the script in Powershell and not EMS as the script does other things.
How do I convert the value to a number?
TIA
Andy

If you're using implicit remoting (it sounds like you are), that's going to be a [string], so you'll need to use string methods.
'1.008 GB (1,082,195,968 bytes)' -replace '^([0-9.]+).+','$1'
1.008

This is very easy, just make a cast and divide with 1GB.
PS C:\> [int]"1082195968"/1GB
1,00787353515625
Or in your case
([int](get-mailboxdatabase xxx -status).databasesize)/1GB

You can convert numbers to the various number types using standard .NET framework types and conversions. For example:
> $var = [decimal]::Parse("1.008")
> $var.GetType().Name
Decimal
Extracting just that portion of the string from the input is a separate issue, and it's hard to tell from your question which is giving you trouble. To get just the number, you might use something like this:
$input = (get-mailboxdatabase xxx -status).databasesize
$strSize = $input.Split(' ')[0]
Or you can use a regular expression. It all depends on how variable your size is.

try this:
$val= (get-mailboxdatabase xxx -status).databasesize
[decimal]([regex]::Match( $val, '(^.+)GB' )).groups[1].value

Related

Call a .NET method with part of the method name replaced by a variable

Here is a simple command that I would like to use a variable in.
(Get-Date).AddDays(-30)
What I'm trying to do is substitute a variable $Time for the word Days.
$Time="Days" OR $Time="Hours"
Then run something like this:
(Get-Date).Add$Time(-30)
Is there a simple way to do this in 1 line?
Obviously I could write an If statement to do this but it seems like there should be a way to make this work in a single command.
You had the right idea, except you need to use the method name 100% from a string or not as a string.
You can do what you seek like this.
$Time = "Days"
(Get-Date)."Add$Time"(-30)
Or
$Time = "AddDays"
(Get-Date).$Time(-30)

PowerShell Digits

Trying to export current temperature from the XML to a text file. The results are are "xx.y" but I only need xx exported to the text file. I've tried several commands but keep striking out. Any ideas?
([xml](Invoke-WebRequest -URI http://w1.weather.gov/xml/current_obs/KSJC.xml).Content).current_observation.temp_f | Out-File c:\temperature.txt
i suspect i am misunderstanding something [this seems alarmingly simple], but here are a few ways to do what it seems you want ... [grin]
$Temperature = '12.3'
$Temperature.Split('.')[0]
[int]$Temperature
[math]::Round([decimal]$Temperature, 0)
the output if each is 12.
the 1st uses the string .Split() method and takes the 1st item in the resulting array
the 2nd uses the [int] type accelerator to coerce the string into an int & rounds it in the process
the 3rd uses the [decimal] type accelerator to coerce the string to a decimal number and the [math]::Round() static method to round the number to 0 decimal places

Validate PowerShell variable contents - alphanumeric, dot, dash and length limitation

Can anyone recommend a short and efficient way of validating the contents of the variable $name, so that it will conform with the following:
Only English alphanumeric characters
Dots "." are allowed
Dashes "-" are allowed
Length should not exceed 10 characters
I can think of long, cumbersome ways of doing that, but I would much rather use an elegant implementation. Something like preg_match in php.
I'll be running it with PowerShell on Windows 2012 Server.
Thank you!
try this:
$string="tEst-.gg"
If ($string -match '^[a-z0-9.-]{1,10}$')
{
"OK"
}
If you want autorise empty string, replace {1,10} by {0,10}

How do you convert data types in Windows Powershell?

I'm very new to powershell and am running into walls trying to convert a string to a integer.
If I run the following command: Get-DefaultAudioDeviceVolume it often returns a number that looks something like: 50.05816%, which I have confirmed to be a string. I need to convert this to a whole number integer (50). Obviously I could hard code the integer in my script, but for the purpose of the script I need it to be flexible in it's conversion. The result of the previous test changes and I want to pass along the whole integer further down the line.
Thanks in advance!
If the string contains the % symbol you would need to remove this, then you can use the -as operator to convert to [int]
[string]$vol = "50.05816%"
$vol_int = $vol.Replace('%','') -as [int]
The -as operator is very useful and has many other uses, this article goes through a number of them: https://mcpmag.com/articles/2013/08/13/utilizing-the-as-operator.aspx
Just cast it to integer and replace the "%" with nothing:
[int]$var = (Get-DefaultAudioDeviceVolume).Replace("%","")
Powershell does automatic type casting and starts from the left. So when $var is defined as an integer, it will try to convert the right side to the same type.

Windbg - How can I Dump Strings which match a given filter

One can dump all the string using the following command
!dumpheap -type System.string
How can dump or print only those string which starts or contains a specific "string"
Example. I am only intrested to view the string which contains "/my/app/request"
Use sosex instead of sos for this. It has a !strings command which allows you to filter strings using the /m:<filter> option.
Use !sosex.strings. See !sosex.help for options to filter strings based on content and/or length.
Not sure if !dumpheap supports that. You can always use .logopen to redirect the output to a file and post-process that. For a more elegant (and thus more complicated) solution, you can also use .shell to redirect the command output to a shell process for parsing. Here's an example:
http://blogs.msdn.com/b/baleixo/archive/2008/09/06/using-shell-to-search-text.aspx
You can also see the .shell documentation for more details:
http://msdn.microsoft.com/en-us/library/windows/hardware/ff565339(v=vs.85).aspx
If you really want to go without SOSEX, then try
.foreach (string {!dumpheap -short -type System.String}) { .foreach (search {s -u ${string}+c ${string}+c+2*poi(${string}+8) "mySearchTerm"}) { du /c80 ${string}+c }}
It uses
!dumpheap to get all Strings on .NET heap
.foreach to iterate over them
s to search for a substring
.foreach again to find out if s found something
some offset calculations to get the first character (+c) of the string and the string length (+8) (multiplied by 2 to get bytes instead of characters). Those need to be adapted in case of 64 bit applications
The /c80 is just for nicer output. You could also use !do ${string} instead of du /c80 ${string}+c if you like the .NET details of the String.