Powershell check for any SD \ CF cards (may be multiple), then copy JPG's and CR2 files to different directories - powershell

Goal: I want to automate copying files (JPG and \ or CR2) from multiple SD \ CF cards into my files into my photo storage system.
Problem: I'm more comfortable in Bash than Powershell but WSL2 doesn't yet support USB drives easily.
Question: How can I check if one or more SD \ CF cards are plugged in and check if certain folders are present? In this case, [drive]:\DCIM\100CANON. I've found this question but I can't quite get it working. Ideally, I want to be able to store the output inside a variable that I can then loop through (and run said copy routine)

The .Net namespaces...
Get-WMiObject -Class Win32_Volume | Select Name, DriveLetter, Caption, Label, DevideID, DriveType, Capacity
or
Get-CimInstance -ClassName Win32_Volume | Select Name, DriveLetter, Caption, Label, DevideID, DriveType, Capacity
or use the Win32_DiskDrive, Win32_DiskDrivePhysicalMedia, and use the Model property value.
Partitions : 1
DeviceID : \\.\PHYSICALDRIVE6
Model : SDHC Card
Size : 3964584960
Caption : SDHC Card
... and the Get-PSDrive ...
Get-PSDriive
Or
Get-PSDrive -PSProvider FileSystem
# Results
<#
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
A A:\
C 202.06 23718.91 FileSystem C:\
D 1211.06 123642.32 FileSystem D:\
G 202.06 710.91 FileSystem \\Music\GratefulDead
#>
... cmdlet will list all connected drives as shown.
Filtering drives are documented here:
Filtering get-psdrive to all Local Drives
DriveType is a property of the Win32_Volume structure which enumerates
the type of drive. The value 3 stands for Local Disk. Below is the
full list of values.
0 - Unknown
1 - No Root Directory
2 - Removable Disk
3 - Local Disk
4 - Network Drive
5 - Compact Disk
6 - RAM Disk
Note, that there is no property/option specifically for SD or CF.
So, for your use case, if your SD/CF has root labeled similar to the above, then you can get to them using the Root property value or use the drive size as your target, since in normal cases, your camera/phone, etc storage media would always be below 32GB total maximum size.

Related

Mapping disk number to Cluster Resource in windows server through power shell script

How do I map the disk number with volume in windows powershell script.?
get-disk command results in:
disk_number friendly_name serial_number etc..
get-volume commands results in:
drive_letter friendly_name fstype drivetype etc..
Question 1. Before bringing up volume as a drive letter / mount point name, how do I know if I am working on the right disk?
Question 2. In cluster failover networks, If I have a disk of 10 GB and is made online, Windows automatically assigns Cluster resource name as "Cluster Disk X" when I do Get-ClusterAvaialableResource. Now how do I map the Cluster resource to the disk number?
With the below cmdlets, I find no property/parameter in common.
a)Get-ClusterResource "Cluster Disk X" |Format-List -Property *
b)Get-Disk -Number "Y" |Format-List -Property *
The situation is little twisted, when I have the disks as mount points under Drive Letter Z:
I am more interested in finding solution to Question2).
Need some pointers. Thanks a lot in advance.

Fetching values from Net Use command using powershell

I am trying to get network mapped drives using below commands.
Get-WmiObject -Class Win32_MappedLogicalDisk | %{$_.Name}
Get-WmiObject -Class Win32_MappedLogicalDisk | %{$_.ProviderName}
This works in some system however does not in other systems(may be powershell version issue) So I thought of using net use command. However, I am unable to fetch the values or not sure how to get the values displays when i type 'net use'
when I type net use I get status, Local, Remote and Network column. I tried to use the below command to get the field values.
net use | select local.
but I get blank or nothing
Used below command.
net use | select local.
Need to get Local and Remote values from net use command.
See this for parsing legacy console output ---
How to Convert Text Output of a Legacy Console Application to PowerShell Objects
Yet, along with what LotPings gave you already. Your query could be a duplicate of this ...
Equivalent of net use (to list computer's connections) in powershell?
... and it's accepted answer
# For the mapped logical drive you can use WMI class Win32_MappedLogicalDisk :
Get-WmiObject Win32_MappedLogicalDisk
# Here is another way with Win32_LogicalDisk :
PS C:\> Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = 4"
DeviceID : V:
DriveType : 4
ProviderName : \\jpbdellf1\c$
FreeSpace :
Size :
VolumeName :
# Edited
# You are right, you can get what you need with Win32_NetworkConnection :
Get-WmiObject Win32_NetworkConnection
LocalName RemoteName ConnectionState Status
--------- ---------- --------------- ------
\\jpbasusf1\temp Connected OK
# On Seven or W2K8 be careful to call this with the same user that run the NET USE because it's a session information.
How about using get-psdrive (the root header actually matches the displayroot property)?
get-psdrive | where displayroot -like '\\*'
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Y 91.84 7.82 FileSystem \\server....
Depending on the PowerShell versions available you might encounter similar problems with
Get-SmbMapping which wraps the CimClass: ROOT/Microsoft/Windows/SMB:MSFT_SmbMapping.
But has otherwise an output resembling net use.
To process the real net use output and convert to an object with properties,
you may use:
$SmbMapping = (net use) -like '* \\*' | ForEach-Object {
$Status,$Local,$Remote,$Null = $_ -split ' +',4
[PSCustomObject]#{
Status = $Status
Local = $Local
Remote = $Remote
}
}
This works at least in my German locale Win10.
(Not sure about different status messages in other locales.)

Getting disk usage using powershell command

I am trying to get the disk usage using the below command.
C:\Users\arjun> (Get-PSDrive $drivename)
WARNING: column "CurrentLocation" does not fit into the display and was removed
.
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
A FileSystem A:\
Alias Alias
C 45.29 4.70 FileSystem C:\
cert Certificate \
D 36.86 13.14 FileSystem D:\
E 230.36 19.64 FileSystem E:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
T 10.84 39.16 FileSystem T:\
Variable Variable
WSMan WSMan
Y FileSystem Y:\
I am getting the above result, however when I try to fetch one column its failing.
(Get-PSDrive $drivename).Used..
Note that is is legacy windows system. Is there any alternative for this. I tried the same command on non-legacy systems and its works fine.
Hello I see what you are doing there might seem rather logical but powershell doesn't know which drive you are defining. So what you should be doing is the following.
$drivename.PSDrive.Used
This way it will exactly know which drive is meant. As Output you will get Byte so you should propably save it into an Variable and then format it.
What you should also be aware of is saving the variable $drivename correctly. That mean that you should save it alike this.
$drivename = Get-Item "C:\Users\arjun"
So it could be that you perhaps forgot to save the object instead of only the path name, or maybe you forgot the quotemarks. Please be sure to check this and mark this answered if it helped you out.

Net view - get Just ' Share Name'

I need to get all of the shares name in some storage's.
Im using Net view $StorageName and it's show The result in a Table format :
Share name Type Used as Comment
----------------------------------------------------------------------------
Backups Disk
CallRecordings Disk
Download Disk System default share
home Disk Home
homes Disk System default share
Installs Disk
Justin Disk Copy of files from Justin laptop
michael Disk
Multimedia Disk System default share
Network Recycle Bin 1 Disk [RAID5 Disk Volume: Drive 1 2 3 4]
Public Disk System default share
Qsync Disk Qsync
Recordings Disk System default share
Sales Disk Sales Documents
SalesMechanix Disk
Server2012 Disk Windows Server 2012 Install Media
Usb Disk System default share
VMWareTemplates Disk
Web Disk System default share
The command completed successfully.
Thats good, but I need Just the Share Name.
I need Help please.
Thanke You!
Here is one way you could do it with the net view output:
(net view $StorageName | Where-Object { $_ -match '\sDisk\s' }) -replace '\s\s+', ',' | ForEach-Object{ ($_ -split ',')[0] }
Basically that is saying find the lines that have Disk surrounded by whitespace just in case something else might have Disk in the name. Then replace multiple spaces with a comma. Then, for each of those lines, split again by the comma and take the first value which would be the share name.
If you are on a Windows 8/2012 or newer system (and attempting to enumerate shares on other Windows systems), you could use a CIM session along with Get-SmbShare instead of net view which would return the results as objects and allow you to select the fields you want in the native PowerShell way.
For example:
$cimSession = New-CimSession $StorageName
Get-SmbShare -CimSession $cimSession | Select Name
Name
----
Admin
ADMIN$
C$
IPC$
J$
print$
Public
Software
Another option for parsing the net view output that relies on positioning rather than regular expression matching. personally I feel it's a bit easier to read and just as reliable.
function get-shares {
param($server)
$rawShares = net view \\$server
$shares = $rawShares[7..($s.Count - 3)]
$shares | . {process{$_.substring(0,($_.indexof(" ")))}}
}

powershell and diskpart

In short I have a volume that I need to assign a drive letter to (using diskpart). The problem now comes in that the volume does not remain the same. You enter disk part a do a "list volume" and the specific volume would be volume 0, then "exit". Enter again and the do a "list volume" again and this time it is volume 4. And so it continues.
Now if this was done by a person it would not be an issue, however this is an automated task, that will "disconnect" the volume on windows 2003 and used on other servers and mounted again on the windows 2003 server.
I'm trying to write a script in powershell that will be able to identify the volume based on a few unique field(s). The problem comes in that I'm getting stuck on interpreting the output of diskpart's "list volume" command with powershell.
The following command provides the output that I need to work with but there after I'm lost.
cls
$dp = "list volume" | diskpart | ? { $_ -match "^ [^-]" }
$dp | format-table -auto
and this is the output it provides and the volume that I'm looking for is Volume 1.
Volume ### Ltr Label Fs Type Size Status Info
Volume 0 F DVD-ROM 0 B Healthy
*Volume 1 Partition 100 GB Healthy*
Volume 2 E DATA NTFS Partition 547 GB Healthy
Volume 3 C OS NTFS Partition 39 GB Healthy System
Volume 4 D APPS NTFS Partition 98 GB Healthy
Can anybody help me in the right direction here please. I'm at my tether's end.
Yes I got it!!
Here is the answer. Using VB Script I managed to create a script that did what I was looking for, this I then translated to Powershell and below is the script.
$drive = gwmi Win32_Volume | where {$_.DeviceID -like "*b0f012f6-82b1-11df-a41c-001f29e8f0be*"}
$drive.AddMountPoint("T:\")
$drive.DriveLetter = "T:"
$drive.Put_
$drive.Mount()
The Device ID I obtained by running the following script:
# get volumes on this system
$volumes = get-wmiobject Win32_Volume
# display volume info
# There are {0} volumes on this system, as follows: " -f ($volumes.length)
# Iterate through volumes and display information
foreach ($vol in $volumes) {
"Volume: {0}" -f ++$i
"============================="
$vol | Format-List Access,Automount,Availability,BlockSize,BootVolume,Capacity,Caption,Compressed,ConfigManagerErrorCode,ConfigManagerUserConfig,CreationClassName,Description,DeviceID,DirtyBitSet,DriveLetter,DriveType,ErrorCleared,ErrorDescription,ErrorMethodology,FileSystem,FreeSpace,IndexingEnabled,InstallDate,Label,LastErrorCode,MaximumFileNameLength,Name,NumberOfBlocks,PageFilePresent,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,Purpose,QuotasEnabled,QuotasIncomplete,QuotasRebuilding,SerialNumber,Status,StatusInfo,SupportsDiskQuotas,SupportsFileBasedCompression,SystemCreationClassName,SystemName,SystemVolume
}
from a post on msdn on the class Win32_Volume.
I hope this might help somebody else Thank you to everybody that help!
You can just use Powershell and WMI to set the drive letter. Shoudn't need diskpart unless you are doing something else (I'm unfamiliar with that tool)
So (assuming you are trying to set the drive letter of the one volume that doesn't have a letter) this should work:
$drive = gwmi Win32_Volume | where {$_.DriveLetter -eq ""}
$drive.DriveLetter = "X:"
$drive.Put()
If you aren't sure about the drive, just query it first and make sure you are only getting the one you want:
gwmi Win32_Volume | where {$_.DriveLetter -eq ""}
Yep. This is a "feature" of diskpart.
Suggestions from MS (not very useful in your case)
Keep the Disk Management console
(Diskmgmt.msc) running while you
process scripts. Or, keep an instance
of the Diskpart.exe utility running
in the background while you process
scripts. When you do this, the volume
numbers should not change between
instances of the Diskpart.exe
utility. Use the volume Label
information instead of the volume
number to track particular volumes.
See bug report here.