Archive for the ‘ Uncategorized ’ Category

Upload files to SharePoint 2010 using PowerShell or C#

I had a need to upload an XML file to a SharePoint 2010 library.  Here’s how I finally got it to work:

PowerShell

 

$destinationurl = "https://my.sharepointserver.com/sites/my-site-name/my-doc-library/my-folder/filename.xml"

$sourceurl = "c:\temp\filename.xml"

$wc = new-object system.net.webclient
[byte[]] $response = $wc.uploadfile($destinationurl, "PUT", $sourceurl)

 

I’m afraid it doesn’t provide much in the way of validation, but it works.  Here’s the same thing in C#, but which takes the source and destination urls as arguments. Obviously the PowerShell approach could be wrapped up in a script that does the same thing.  Sorry about the formatting. I’m really not digging this blog theme.  I need to change it.

C#

 

using System;
using System.Net;

namespace spcopy
{
    class Program
    {
        static void Main(string[] args)
        { 

  // Set the local file
            //string sourceUrl = @"C:\test.txt";
            string sourceUrl = args[0];

            // Set destination file
            //string destinationUrl = @"https://my.sharepointserver.com/sites/my-site-name/my-doc-library/my-folder/filename.xml"
            string destinationUrl = args[1];

            WebClient wc = new WebClient();
            wc.UseDefaultCredentials = true;
            byte[] response = wc.UploadFile(destinationUrl, "PUT", sourceUrl);

            sourceUrl = null;
            destinationUrl = null;
            wc.Dispose();
            response = null;

            GC.Collect();
        }
    }
}

Advertisements

Upgrading 2008 R2 using DISM in a KMS licensing environment

Today I had to upgrade a server from 2008 R2 Standard to Enterprise so I could install Failover Clustering. You can do this using the DISM tool provided in the OS, but there are some caveats associated with doing so in a KMS licensed environment. Specifically, DISM requires that you provide a product key, which we obviously don’t have, and don’t want to use because we’re leveraging a KMS server. So, a few quick notes (blatantly stolen from this article over at ‘Life of A Geek Admin’):

A few caveats:

  • You can only do upgrades. You CANNOT downgrade
  • The server you upgrade cannot be a domain controller (demote, upgrade, promote)
  • This works on Standard, Enterprise edition, both full & core installations.
  • You cannot switch form core to full or vice versa. It’s edition upgrade only, not for switching type of install.

Get your current version information:

DISM /online /Get-TargetEditions

Now perform your upgrade using a public product key:

DISM /online /Set-Edition: /ProductKey: #####-#####-#####-#####-#####

And here are the public product keys we can use:

  • Windows Server 2008 R2 HPC Edition: FKJQ8-TMCVP-FRMR7-4WR42-3JCD7
  • Windows Server 2008 R2 Datacenter: 74YFP-3QFB3-KQT8W-PMXWJ-7M648
  • Windows Server 2008 R2 Enterprise: 489J6-VHDMP-X63PK-3K798-CPX3Y
  • Windows Server 2008 R2 for Itanium-Based Systems: GT63C-RJFQ3-4GMB6-BRFB9-CB83V
  • Windows Server 2008 R2 Standard: YC6KT-GKW9T-YTKYR-T4X34-R7VHC
  • Windows Web Server 2008 R2: 6TPJF-RBVHG-WBW2R-86QPH-6RTM4

A change of plan…

Today I finally decided what direction I’m going to take my blog in (for now). I will be picking a subject, breaking it up into small components, or episodes, and then writing about that subject one episode at a time. Why am I doing this? Because this blog is for me. I lost track of that somewhere along the way. I’m happy to share the information, and enjoy writing my content as if it were directed at a larger audience, but at the end of the day I’m probably the only one referencing it on a regular basis. Given that, my blog’s primary purpose should be to act as a learning and referential tool for myself, and I think this approach will most readily benefit me. With any luck, the very few people with very poor taste who actually debase themselves to read my work from time to time (Ben & Omar) might get some value out of it. So, what’s the first series going to be about? Well, without further ado, I’m pleased to announce the debut of… wait for it…

Series 1 – PowerShell 3.0!

I’ve just recently made the leap to Windows 8 on my production machine, and am starting to work with Windows Server 2012. I like it! I wish I had a touch-screen, but that aside, I still really like it. It really grew on me once I got past the disgruntled period where I was resentful about having to learn the new interface. Now I don’t think I would want to go back.

So, with Windows 8 comes PowerShell 3.0, and with PowerShell 3.0 comes lots of new things to learn! I’m going to write about these as often as I can, and have broken it down into the subjects below, thought I do reserve the right to update this list as I see fit, and you can expect that I will.

That said, on with the show! Let’s see what’s in the lineup (I’ll add links to the posts as I complete them):

Episode

Title

1

PowerShell 101 – Part 1

2

PowerShell 101 – Part 2

3

PowerShell 101 – Part 3

4

Microsoft.PowerShell.*

5

Microsoft.Powershell.Core : Enable-PSSessionConfiguration

6

Microsoft.Powershell.Core : New-PSTransportOption

7

Microsoft.Powershell.Core : Enable-PSRemoting

8

Microsoft.Powershell.Core : Remove-Job

9

Microsoft.Powershell.Core : Enter-PSSession

10

Microsoft.Powershell.Core : Exit-PSSession

11

Microsoft.Powershell.Core : Set-StrictMode

12

Microsoft.Powershell.Core : Export-Console

13

Microsoft.Powershell.Core : Set-PSSessionConfiguration

14

Microsoft.Powershell.Core : Export-ModuleMember

15

Microsoft.Powershell.Core : Set-PSDebug

16

Microsoft.Powershell.Core : ForEach-Object

17

Microsoft.Powershell.Core : Out-Default

18

Microsoft.Powershell.Core : Out-Host

19

Microsoft.Powershell.Core : Out-Null

20

Microsoft.Powershell.Core : Get-PSSnapin

21

Microsoft.Powershell.Core : Save-Help

22

Microsoft.Powershell.Core : Get-PSSessionConfiguration

23

Microsoft.Powershell.Core : Resume-Job

24

Microsoft.Powershell.Core : Get-PSSession

25

Microsoft.Powershell.Core : Get-Command

26

Microsoft.Powershell.Core : Receive-Job

27

Microsoft.Powershell.Core : Receive-PSSession

28

Microsoft.Powershell.Core : Register-PSSessionConfiguration

29

Microsoft.Powershell.Core : Get-Module

30

Microsoft.Powershell.Core : Remove-PSSnapin

31

Microsoft.Powershell.Core : Remove-PSSession

32

Microsoft.Powershell.Core : Get-Help

33

Microsoft.Powershell.Core : Get-History

34

Microsoft.Powershell.Core : Remove-Module

35

Microsoft.Powershell.Core : Disconnect-PSSession

36

Microsoft.Powershell.Core : Disable-PSSessionConfiguration

37

Microsoft.Powershell.Core : Get-Job

38

Microsoft.Powershell.Core : New-PSSessionOption

39

Microsoft.Powershell.Core : Add-History

40

Microsoft.Powershell.Core : Where-Object

41

Microsoft.Powershell.Core : Add-PSSnapin

42

Microsoft.Powershell.Core : Invoke-History

43

Microsoft.Powershell.Core : Wait-Job

44

Microsoft.Powershell.Core : Update-Help

45

Microsoft.Powershell.Core : Clear-History

46

Microsoft.Powershell.Core : Unregister-PSSessionConfiguration

47

Microsoft.Powershell.Core : Invoke-Command

48

Microsoft.Powershell.Core : Test-PSSessionConfigurationFile

49

Microsoft.Powershell.Core : Disable-PSRemoting

50

Microsoft.Powershell.Core : Connect-PSSession

51

Microsoft.Powershell.Core : Import-Module

52

Microsoft.Powershell.Core : New-Module

53

Microsoft.Powershell.Core : Test-ModuleManifest

54

Microsoft.Powershell.Core : Start-Job

55

Microsoft.Powershell.Core : New-ModuleManifest

56

Microsoft.Powershell.Core : Stop-Job

57

Microsoft.Powershell.Core : New-PSSessionConfigurationFile

58

Microsoft.Powershell.Core : New-PSSession

59

Microsoft.Powershell.Core : Suspend-Job

60

Microsoft.Powershell.Diagnostics : Get-Counter

61

Microsoft.Powershell.Diagnostics : Import-Counter

62

Microsoft.Powershell.Diagnostics : New-WinEvent

63

Microsoft.Powershell.Diagnostics : Get-WinEvent

64

Microsoft.Powershell.Diagnostics : Export-Counter

65

Microsoft.Powershell.Host : Start-Transcript

66

Microsoft.Powershell.Host : Stop-Transcript

67

Microsoft.Powershell.Management : Push-Location

68

Microsoft.Powershell.Management : Move-ItemProperty

69

Microsoft.Powershell.Management : Register-WmiEvent

70

Microsoft.Powershell.Management : Pop-Location

71

Microsoft.Powershell.Management : Remove-Computer

72

Microsoft.Powershell.Management : New-PSDrive

73

Microsoft.Powershell.Management : New-Item

74

Microsoft.Powershell.Management : Remove-EventLog

75

Microsoft.Powershell.Management : New-Service

76

Microsoft.Powershell.Management : New-ItemProperty

77

Microsoft.Powershell.Management : Move-Item

78

Microsoft.Powershell.Management : New-WebServiceProxy

79

Microsoft.Powershell.Management : Add-Computer

80

Microsoft.Powershell.Management : Restore-Computer

81

Microsoft.Powershell.Management : Remove-ItemProperty

82

Microsoft.Powershell.Management : Split-Path

83

Microsoft.Powershell.Management : Start-Process

84

Microsoft.Powershell.Management : Start-Service

85

Microsoft.Powershell.Management : Start-Transaction

86

Microsoft.Powershell.Management : Stop-Computer

87

Microsoft.Powershell.Management : Stop-Process

88

Microsoft.Powershell.Management : Stop-Service

89

Microsoft.Powershell.Management : Suspend-Service

90

Microsoft.Powershell.Management : Test-ComputerSecureChannel

91

Microsoft.Powershell.Management : Test-Connection

92

Microsoft.Powershell.Management : Test-Path

93

Microsoft.Powershell.Management : Undo-Transaction

94

Microsoft.Powershell.Management : Use-Transaction

95

Microsoft.Powershell.Management : Wait-Process

96

Microsoft.Powershell.Management : Write-EventLog

97

Microsoft.Powershell.Management : Show-EventLog

98

Microsoft.Powershell.Management : Remove-Item

99

Microsoft.Powershell.Management : Show-ControlPanelItem

100

Microsoft.Powershell.Management : Set-Service

101

Microsoft.Powershell.Management : Remove-PSDrive

102

Microsoft.Powershell.Management : Remove-WmiObject

103

Microsoft.Powershell.Management : Rename-Computer

104

Microsoft.Powershell.Management : Rename-Item

105

Microsoft.Powershell.Management : Rename-ItemProperty

106

Microsoft.Powershell.Management : Reset-ComputerMachinePassword

107

Microsoft.Powershell.Management : Resolve-Path

108

Microsoft.Powershell.Management : Restart-Computer

109

Microsoft.Powershell.Management : Restart-Service

110

Microsoft.Powershell.Management : Limit-EventLog

111

Microsoft.Powershell.Management : Resume-Service

112

Microsoft.Powershell.Management : Set-Content

113

Microsoft.Powershell.Management : Set-Item

114

Microsoft.Powershell.Management : Set-ItemProperty

115

Microsoft.Powershell.Management : Set-Location

116

Microsoft.Powershell.Management : Set-WmiInstance

117

Microsoft.Powershell.Management : Join-Path

118

Microsoft.Powershell.Management : New-EventLog

119

Microsoft.Powershell.Management : Get-Process

120

Microsoft.Powershell.Management : Get-ComputerRestorePoint

121

Microsoft.Powershell.Management : Complete-Transaction

122

Microsoft.Powershell.Management : Get-Service

123

Microsoft.Powershell.Management : Get-Content

124

Microsoft.Powershell.Management : Get-ControlPanelItem

125

Microsoft.Powershell.Management : Get-PSProvider

126

Microsoft.Powershell.Management : Get-PSDrive

127

Microsoft.Powershell.Management : Enable-ComputerRestore

128

Microsoft.Powershell.Management : Convert-Path

129

Microsoft.Powershell.Management : Get-EventLog

130

Microsoft.Powershell.Management : Disable-ComputerRestore

131

Microsoft.Powershell.Management : Get-Location

132

Microsoft.Powershell.Management : Get-ItemProperty

133

Microsoft.Powershell.Management : Get-Item

134

Microsoft.Powershell.Management : Get-HotFix

135

Microsoft.Powershell.Management : Debug-Process

136

Microsoft.Powershell.Management : Copy-Item

137

Microsoft.Powershell.Management : Copy-ItemProperty

138

Microsoft.Powershell.Management : Get-Transaction

139

Microsoft.Powershell.Management : Get-ChildItem

140

Microsoft.Powershell.Management : Invoke-Item

141

Microsoft.Powershell.Management : Clear-Item

142

Microsoft.Powershell.Management : Invoke-WmiMethod

143

Microsoft.Powershell.Management : Get-WmiObject

144

Microsoft.Powershell.Management : Clear-EventLog

145

Microsoft.Powershell.Management : Add-Content

146

Microsoft.Powershell.Management : Clear-Content

147

Microsoft.Powershell.Management : Clear-ItemProperty

148

Microsoft.Powershell.Management : Checkpoint-Computer

149

Microsoft.Powershell.Security : Set-Acl

150

Microsoft.Powershell.Security : Set-ExecutionPolicy

151

Microsoft.Powershell.Security : ConvertTo-SecureString

152

Microsoft.Powershell.Security : Set-AuthenticodeSignature

153

Microsoft.Powershell.Security : Get-PfxCertificate

154

Microsoft.Powershell.Security : Get-ExecutionPolicy

155

Microsoft.Powershell.Security : Get-Credential

156

Microsoft.Powershell.Security : Get-Acl

157

Microsoft.Powershell.Security : Get-AuthenticodeSignature

158

Microsoft.Powershell.Security : ConvertFrom-SecureString

159

Microsoft.Powershell.Utility : Sort-Object

160

Microsoft.Powershell.Utility : Disable-PSBreakpoint

161

Microsoft.Powershell.Utility : Set-TraceSource

162

Microsoft.Powershell.Utility : Set-Variable

163

Microsoft.Powershell.Utility : Export-Alias

164

Microsoft.Powershell.Utility : Export-Clixml

165

Microsoft.Powershell.Utility : Show-Command

166

Microsoft.Powershell.Utility : Export-Csv

167

Microsoft.Powershell.Utility : Export-FormatData

168

Microsoft.Powershell.Utility : Enable-PSBreakpoint

169

Microsoft.Powershell.Utility : Invoke-WebRequest

170

Microsoft.Powershell.Utility : ConvertTo-Xml

171

Microsoft.Powershell.Utility : Write-Progress

172

Microsoft.Powershell.Utility : Write-Output

173

Microsoft.Powershell.Utility : Write-Host

174

Microsoft.Powershell.Utility : Write-Error

175

Microsoft.Powershell.Utility : Write-Debug

176

Microsoft.Powershell.Utility : Add-Member

177

Microsoft.Powershell.Utility : Add-Type

178

Microsoft.Powershell.Utility : Wait-Event

179

Microsoft.Powershell.Utility : Update-TypeData

180

Microsoft.Powershell.Utility : Update-List

181

Microsoft.Powershell.Utility : Update-FormatData

182

Microsoft.Powershell.Utility : Start-Sleep

183

Microsoft.Powershell.Utility : Unregister-Event

184

Microsoft.Powershell.Utility : Trace-Command

185

Microsoft.Powershell.Utility : Clear-Variable

186

Microsoft.Powershell.Utility : Compare-Object

187

Microsoft.Powershell.Utility : ConvertFrom-Csv

188

Microsoft.Powershell.Utility : ConvertFrom-Json

189

Microsoft.Powershell.Utility : Tee-Object

190

Microsoft.Powershell.Utility : ConvertFrom-StringData

191

Microsoft.Powershell.Utility : ConvertTo-Csv

192

Microsoft.Powershell.Utility : ConvertTo-Html

193

Microsoft.Powershell.Utility : Export-PSSession

194

Microsoft.Powershell.Utility : ConvertTo-Json

195

Microsoft.Powershell.Utility : Unblock-File

196

Microsoft.Powershell.Utility : Set-PSBreakpoint

197

Microsoft.Powershell.Utility : Select-Object

198

Microsoft.Powershell.Utility : Format-List

199

Microsoft.Powershell.Utility : Out-GridView

200

Microsoft.Powershell.Utility : Out-File

201

Microsoft.Powershell.Utility : Get-TraceSource

202

Microsoft.Powershell.Utility : Get-TypeData

203

Microsoft.Powershell.Utility : New-Variable

204

Microsoft.Powershell.Utility : New-TimeSpan

205

Microsoft.Powershell.Utility : Get-UICulture

206

Microsoft.Powershell.Utility : Get-Unique

207

Microsoft.Powershell.Utility : Get-Variable

208

Microsoft.Powershell.Utility : Group-Object

209

Microsoft.Powershell.Utility : Import-Alias

210

Microsoft.Powershell.Utility : New-Object

211

Microsoft.Powershell.Utility : Import-Clixml

212

Microsoft.Powershell.Utility : Import-Csv

213

Microsoft.Powershell.Utility : Import-LocalizedData

214

Microsoft.Powershell.Utility : Import-PSSession

215

Microsoft.Powershell.Utility : Write-Verbose

216

Microsoft.Powershell.Utility : New-Event

217

Microsoft.Powershell.Utility : New-Alias

218

Microsoft.Powershell.Utility : Invoke-Expression

219

Microsoft.Powershell.Utility : Measure-Object

220

Microsoft.Powershell.Utility : Measure-Command

221

Microsoft.Powershell.Utility : Invoke-RestMethod

222

Microsoft.Powershell.Utility : Get-Random

223

Microsoft.Powershell.Utility : Format-Custom

224

Microsoft.Powershell.Utility : Out-Printer

225

Microsoft.Powershell.Utility : Read-Host

226

Microsoft.Powershell.Utility : Format-Table

227

Microsoft.Powershell.Utility : Set-Date

228

Microsoft.Powershell.Utility : Format-Wide

229

Microsoft.Powershell.Utility : Set-Alias

230

Microsoft.Powershell.Utility : Send-MailMessage

231

Microsoft.Powershell.Utility : Select-Xml

232

Microsoft.Powershell.Utility : Select-String

233

Microsoft.Powershell.Utility : Get-Alias

234

Microsoft.Powershell.Utility : Get-Culture

235

Microsoft.Powershell.Utility : Get-Date

236

Microsoft.Powershell.Utility : Get-Event

237

Microsoft.Powershell.Utility : Remove-Variable

238

Microsoft.Powershell.Utility : Remove-TypeData

239

Microsoft.Powershell.Utility : Get-EventSubscriber

240

Microsoft.Powershell.Utility : Get-FormatData

241

Microsoft.Powershell.Utility : Remove-PSBreakpoint

242

Microsoft.Powershell.Utility : Get-Host

243

Microsoft.Powershell.Utility : Remove-Event

244

Microsoft.Powershell.Utility : Get-Member

245

Microsoft.Powershell.Utility : Register-ObjectEvent

246

Microsoft.Powershell.Utility : Register-EngineEvent

247

Microsoft.Powershell.Utility : Get-PSBreakpoint

248

Microsoft.Powershell.Utility : Get-PSCallStack

249

Microsoft.Powershell.Utility : Out-String

250

Microsoft.Powershell.Utility : Write-Warning