I am trying to get the .Net version from the system and trying to check does my system have .Net version greater than or equal to 4.6 or not. Below is the code. From the code I am able to retrieve the release version. I am facing a problem in the if condition.
$myArray = #()
$myArray = #(Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
Get-ItemProperty -Name Release,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}' } |
Select Release)
Write-Output $myArray[0]
Write-Output $myArray[1]
$var1 = 393295
foreach ($var in $myArray) {
Write-Output $var
if ($var -ge $var1) {
Write-Output same
} else {
Write-Output NOT same
}
}
I am getting this error:
Cannot compare "#{Release=461808}" to "393295" because the objects are not the
same type or the object "#{Release=461808}" does not implement "IComparable".
At C:\Users\Desktop\DotNet.ps1:13 char:9
+ if ($var -ge $var1) {
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : PSObjectCompareTo
Change the foreach line as
foreach($var in $myArray.Release)
Related
I came across PowerShell script to log Microsoft Office version of remote computer on domain. I want to run it as logon script to I modified OpenRemoteBaseKey to OpenBaseKey and this is the code:
$version = 0
$reg = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Default')
$reg.OpenSubKey('software\Microsoft\Office').GetSubKeyNames() |% {
if ($_ -match '(\d+)\.') {
if ([int]$matches[1] -gt $version) {
$version = $matches[1]
}
}
}
if ($version) {
Add-Content -Path \\server\share\oversion.txt -Value "$env:computername $env:username : $version"
}
else {
Add-Content -Path \\server\share\oversion.txt -Value "$env:computername $env:username : 0"
}
but now I receive error:
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $reg.OpenSubKey('software\Microsoft\Office').GetSubKeyNames() |% {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
but not sure what this means since GetSubKeyNames seems valid: https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.getsubkeynames?view=netframework-4.7.2, also it works with OpenRemoteBaseKey, can someone point me to right direction, please?
I'm not sure why OpenRemoteBaseKey works, but OpenBaseKey doesn't because I cannot reproduce that..
You might however try the more Powershell way of doing this:
$version = 0
Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Office' -Name | Where-Object {$_ -match '(\d+)\.\d+'} | ForEach-Object {
$version = [math]::Max([int]$_, $version)
}
Add-Content -Path \\server\share\oversion.txt -Value "$env:COMPUTERNAME $env:USERNAME : $version"
My script is working, but I'm getting this error:
The property 'Name' cannot be found on this object. Verify that the property exists.
I'm looping to copy folders. Any idea what is going on?
$source50 = "c:\folder\"
$destination50 = "c:\folder1\"
for ($i = 1; $i -le 7; $i++) {
$d = ((Get-Date).AddDays( + $i))
$d2 = $d.ToString("yyyy-MM-dd")
$v = Get-ChildItem $source50 -Recurse -Include "$d2"
foreach ($file in $v)
{
if ( Test-Path $v.FullName)
{
if (-not (Test-Path -Path $destination50$d4)){
New-Item -ItemType directory -Path $destination50$d4
}
Write-Output "Copy ok " $v.FullName
$bd= Copy-Item $v.FullName -Destination $destination50$d4
break
}
}
The code is giving the error:
ok C:\folder\2017-12-27
ok C:\folder\2017-12-28
The property 'Name' cannot be found on this object. Verify that the property
exists.
At line:16 char:11
+ if ($v.Name -eq $d2) {
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
ok C:\folder\2017-12-30
The property 'Name' cannot be found on this object. Verify that the property
exists.
the $v.Name throw exception because its an array object and not "System.IO." that has the property Name.
try using this code:
$source50 = "c:\folder\"
$destination50 = "c:\folder1\"
for ($i = 1; $i -le 7; $i++)
{
$d = ((Get-Date).AddDays( + $i))
$d2 = $d.ToString("yyyy-MM-dd")
$v = Get-ChildItem $source50 -Recurse -include "$d2"
foreach($file in $v)
{
if ($file.Name -eq $d2)
{
Copy-Item $file.FullName -Destination $destination50 -Force
Write-Host "ok" $file.FullName | Out-File -FilePath c:\folder\logggg.txt
}
}
}
I am trying to add 400 computer to a collection and I have an error running the powershell in SCCM. I try to change the . to _ but also encounter the same error.
Method invocation failed because [System.Char] does not contain a method named 'Split'.
At line:8 char:5
+ $collectionname = $filenames.Name[$x].Split(".")[0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Powershell:
#Set path to collection directory
$collectiondir = "D:\Collections\"
#Pull only .TXT files into array
$filenames = Get-ChildItem $collectiondir* -include *.txt
for ($x=0; $x -lt ($filenames.Length); $x++) {
$collectionname = $filenames.Name[$x].Split(".")[0]
$collectionname
#Add new collection based on the file name
try {
New-CMDeviceCollection -Name $collectionname -LimitingCollectionName "All Systems"
}
catch {
"Error creating collection - collection may already exist: $collectionname" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
}
#Read list of computers from the text file
$computers = Get-Content $filenames[$x]
foreach($computer in $computers) {
try {
Add-CMDeviceCollectionDirectMembershipRule -CollectionName $collectionname -ResourceId $(get-cmdevice -Name $computer).ResourceID
}
catch {
"Invalid client or direct membership rule may already exist: $computer" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
}
}
}
You must have made a mistake following my hints, both do work:
$collectiondir = "D:\Collections\"
#Pull only .TXT files into array
$filenames = Get-ChildItem -Path $collectiondir -Filter *.txt
for ($x=0; $x -lt ($filenames.Length); $x++) {
$collectionname = $filenames[$x].Name.Split(".")[0]
$collectionname
}
"-----"
ForEach ($file in $filenames){
$collectionname = $file.Name.Split(".")[0]
$collectionname
}
If you wanted to split the extension a better way is to simply use BaseName instead of Name
After trying a number of approaches, including what seemed an excellent suggestion at http://www.bdevuyst.com/powershell-path-msbuild-exe/, which gave me an error, I tried to break it down. Though I get the latest MSBuild registry key (14.0), I still get this error when I try to extract the path to MSBuild:
Get-ItemProperty : Cannot find path 'C:\USERS\user\Desktop\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\ToolsVersions\14.0' because it does not exist.
At C:\USERS\mtroi\Desktop\VSS_POC1_Setup.ps1:529 char:5
+ Get-ItemProperty -Path $MsBuildVersion -Name MSBuildToolsPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\USERS\mtroi\...lsVersions\14.0:String) [Get-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
# 32bit or 64bit local OS?
if($ENV:PROCESSOR_ARCHITECTURE -eq "x86")
{$HKLMpath = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\"}
elseif($ENV:PROCESSOR_ARCHITECTURE -eq "AMD64")
{$HKLMpath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSBuild\ToolsVersions\"}
else
{Write-Host "Local processor architecture not supported. Exiting installation..."; EXIT}
# Which path (version) of local MSBuild?
$_decSep = [System.Threading.Thread]::CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator;
$MsBuild1 = #(Get-ChildItem -Path $HKLMpath | Where { $_.Name -match '\\\d+.\d+$' })
ForEach ($build in $MsBuild1)
{
$Expression=$Expression+","+[System.Convert]::ToDecimal($build.Name.Substring($build.Name.LastIndexOf("\") + 1))
}
$MsBuildVersion = Get-ChildItem -Path $HKLMpath | Where { $_.Name -match '\\\d+.\d+$' } |
Sort-Object -Property #{Expression=$Expression} -Descending |
Select-Object -First 1 #| Get-ItemProperty -Name MSBuildToolsPath
Get-ItemProperty -Path $MsBuildVersion -Name MSBuildToolsPath
Where am I going wrong with the below script...it checks for if files are of zero (0) bytes and then if they are it moves them to a folder.
It works fine outside of the IF statement but when I try it in the below it fails copying the files and displays the below error:
Move-Item : Cannot bind argument to parameter 'Path' because it is
null. At C:\Tools\jon\testing_scheduled.ps1:109 char:11
+ Move-Item <<<< $moving "$scheduledpath\Move_empty"
+ CategoryInfo : InvalidData: (:) [Move-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand
filter gettheheckout([string]$path = '.')
{
$move = Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0} | Foreach-Object {$_.fullName}
}
$moving = gettheheckout
$check = #(Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0})
if ($check.length -eq 0)
{
Write-host = "No files to move - Script Completed" -ForegroundColor Cyan
}
else
{
Move-Item $moving "$scheduledpath\Move_empty"
Write-Host "Script Completed - Use Excel to Filter on commas - Have a nice day!" -ForegroundColor Cyan
}
change this:
filter gettheheckout([string]$path = '.')
{
Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0} | Foreach-Object {$_.fullName}
}
and be sure $scheduledpath is a global scope variable and have a value.