I'm trying to automate snapshot checking DS free space. Its getting tricky for VMs with multiple DS attached. Script takes multiple snapshots for such VMs if condition satisfies. Please help me to understand where its going wrong.
Consolidating free space:
$free = (Get-Datastore -VM $vm | Select #{N="FreeSpace";E={[math]::Round(($_.FreeSpaceMB)*100/($_.CapacityMB),0)}})
Now checking if free space is available in each DS where VM connected:
foreach ($ds in $free.FreeSpace)
{
if (($ds -gt 25)
{
get-vm $vm | new-snapshot -name "$cmr.$date" -Description $description
}
}
If I understand the question properly, regarding dealing with multiple datastores... I would take a look at introducing a Sort-Object after the initial Get-Datastore which is based on the FreeSpaceMB property, then only selecting the first datastore (which should have the least amount of free space available) and performing your calculation based on that.
Untested example:
$free = (Get-Datastore -VM $vm | Sort-Object -Property FreeSpaceMB | Select-Object -Property #{N="FreeSpace";E={[math]::Round(($_.FreeSpaceMB)*100/($_.CapacityMB),0)}} -First 1)
Related
I have more than 1100 Windows devices registered on Intune, some users had their laptop replaced, however the device replaced was not deleted. How can I generate a list of UPN that contains more than 1 device? I will need this check this list and then remove it from On-Prem AD, AAD, Intune.
I was trying to create a PowerShell script, but I am not finding a way to do that.
$intuneDevices = Get-IntuneManagedDevice | Get-MSGraphAllPages
$windevices = $intuneDevices | Where-Object { $.operatingSystem -eq "Windows" }
$windevices | **Where-Object {$.userPrincipalName -ge '1'}* | Select DeviceName, userPrincipalName
*
I am not sure exactly how I can bring userPrincipalName with more than 1 device.
I hope it make sense and someone can help
Thanks in advance
TZ
I'm trying to take the value of free space from a drive, this is necessary to do an automatict procedure in a database server.
I got this script:
$query_drive_mount_point = #"
select distinct
convert(varchar(512), b.volume_mount_point) as [volume],
convert(varchar(512), b.logical_volume_name) as [logical_volume]
from sys.master_files as [a]
CROSS APPLY sys.dm_os_volume_stats(a.database_id, a.[file_id]) as [b]
"#
[regex]$get_drive = '\w\:\\'
[regex]$get_drive_name = '\w'
[regex]$get_drive_space = '\d'
$mount_point = Invoke-Sqlcmd -ServerInstance "$server_ip\$sql_engine,$sql_port" -Username "$sql_user" -Password "$sql_password" -Database Master -Query "$query_drive_mount_point"
$get_disk = $get_drive.Matches($mount_point) | Foreach-Object {$_.Value}
$get_disk_name = $get_drive_name.Matches($get_disk) | Foreach-Object {$_.Value}
$size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -Property Free
[int]$size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach-Object {$_.Value}
$size_giga = ( ( ( $size_bytes )/1024 )/1024 )/1024
This code runs without problem until this line:
[int]$size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach-Object {$_.Value}
It throws this error:
There was not found overload for "Matches" and args numbers is "1".
En línea: 1 Carácter: 1
+ $size_bytes = $get_drive_space.Matches($size_bytes_string) | ForEach- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
The last error is a traduction made by me, the os is in spanish.
Again, the objective is to store only the free space value.
You need to select the value from the property you need, so this line:
$size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -Property Free to
Should be changed to the following:
$size_bytes_string = Get-PSDrive $get_disk_name | Select-Object -ExpandProperty Free
Alternatively you can format it as follows:
($size_bytes_string = Get-PSDrive $get_disk_name).Free
If you have PS version 5:
PS C:\> (($PSVersionTable).PSVersion)
Major Minor Build Revision
----- ----- ----- --------
5 1 18362 145
if not version 5, try to import the storage module.
You can get this information easier by using the below commandlet:
Get-disk # the command name
Get-Command -Module storage -Verb get -Noun disk # module
onliner code to select the size of the first hdd (disk 0):
[math]::Round((Get-Disk -Number 0).Size /1GB)
- Edited :
=========================================================================
if you load this assembly, you can get all the information you need,
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$sql = New-Object ('Microsoft.SqlServer.Management.Smo.Server')
You can run this commandlet to find what information you can get :
(($sql | gm) | measure).count
For example :
write-host $dbs.Parent
$dbs=$s.Databases
I believe you need a property called filegroup to get information about ldf&mdf files.
I'm experimenting for the first time PowerCli on my VSphere environment.
I'm trying the Get-Vm filtering the results in this way and it's working fine:
Get-Vm | where MemoryGB -eq "8"
but if I try the same syntax, filtering by VMHost property, I don't get any result:
Get-Vm | where VMHost -eq "10.0.0.30"
But I have a lot of machines on the host 10.0.0.30, why it's not working?
I can see it if I use this syntax that filters the text output in the end:
Get-Vm | select name,vmhost | findstr -i .30
what I'm doing wrong?
thanks
The VMHost property that you're referring to is actually a VMHost object, so you may have to filter something like Get-VM | where {$_.VMHost.Name -eq '10.0.0.30'} in order to do a string vs. string comparison.
Get-VMHost -Name "10.0.0.30" | Get-VM
VMHost has the property "Name".
So you can pass the name(ip) of VMHost and get the list of all vms on the hosted ip.
Link to cmndlet explanation: https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/Get-VMHost.html#:~:text=Suspend%2DVMHost-,This%20cmdlet%20retrieves%20the%20hosts%20on%20a%20vCenter%20Server%20system,one%2C%20use%20the%20Server%20parameter.
I am trying to get the list of VMWare snapshots whose Uid does not match 'XYZ' using PowerCLI.
I have written the following code to do that.
$body +=echo ""`r`n""`r`n"-----Open VMware snapshots other than Uid 'XYZ'-----"`r`n""`r`n""
Get-VM | Get-snapshot | where {($_.Uid -notmatch 'XYZ')} | ForEach-Object {
$object=New-Object -TypeName PSObject -Property #{
VM_Name = $_.VM
Created = $_.Created
Size = "$([math]::Round($_.SizeGB, 2)) GB"
SnapshotName = $_.name
Description = $_.extensiondata.description
}
$body += $object | ft | out-string
Write-Output $object
}
Running that code in PowerCLI returns all the VMWare snapshots including the one with Uid 'XYZ', which should not happen.
How do I modify the code?
Try using -ne or -notlike in your Where-Object comparison since it appears we're comparing strings and not using Regular Expressions.
Get-VM | Get-snapshot | where {($_.Uid -notlike 'XYZ')} | ForEach-Object ...
I have created the following script that returns the percentage of free space , the total space etc. of every disk for the remote servers the problem is that i want an extra column "warning" that prints No or Yes if the free space is bellow 10% i tried if statement but with no success. Please for your help.
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content .\servers.txt) | Select SystemName,DeviceID,VolumeName,#{Name="Size(GB)";
Expression={"{0:N1}" -f($_.size/1gb)}},#{Name="FreeSpace(GB)";
Expression={"{0:N1}" -f($_.freespace/1gb)}},#{Name=" % Free(GB)";
Expression={"{0:N1}" -f(($_.freespace/$_.size)*100 )}},#{Name=" Warning";
Expression={????????}} |Format-Table -AutoSize |Out-File disk_monitor.txt
You can try something like
#{Name="Warning";Expression={ if((100 / $_.Size * $_.FreeSpace) -lt 10) { "Yes" } else { "No" }} };
This will calculate what percentage of disk space is available (100 / Size * FreeSpace) and if it's less than 10 (as in, percent), will return "Yes" or otherwise "No".